java - NullPointerException in PhoneDirectory programme -


i have come across error in phonedirectory programme. when try , compile arrayphonedirectorytester, builds returns error:

exception in thread "main" java.lang.nullpointerexception @ arrayphonedirectory.addchangeentry(arrayphonedirectory.java:60) @ arrayphonedirectorytester.main(arrayphonedirectorytester.java:17) java result: 1 

from little bit of research online, understand means variable trying use has been set null, , occurs in arrayphonedirectory java file in line 60, , in arrayphonedirectorytester java file in line 17 (both commented in code below.). still unsure variable pointing @ , regarding fixing exception appreciated.

arrayphonedirectory code:

import java.io.*; import java.util.*;  public class arrayphonedirectory implements phonedirectory {  private static final int init_capacity = 100; private int capacity = init_capacity;  //holds telno of directory entries private int size = 0;  //array contain directory entries private directoryentry[] thedirectory = new directoryentry[capacity];  //holds name of data file read private string sourcename = null;  /**  * flag indicate whether directory modified since last loaded  * or saved.  */ private boolean modified = false;  // public interface methods  public void loaddata(string sourcename) {     scanner scan = new scanner(sourcename).usedelimiter("\\z");      while (scan.hasnextline()) {         string name = scan.nextline();         string telno = scan.nextline();          add(name, telno);     } }  /**  * find method called, returning position in array of given  * name.  */ public string lookupentry(string name) {     find(name);     return null;  }  /**  * loop checks every directoryentry inside thedirectory ,  * checks against name , telno given in parameter, if both  * equal, telno upda ted, else added thedirectory  *  */ public string addchangeentry(string name, string telno) {     (directoryentry x : thedirectory) {         if (x.getname().equals(name)) {              //line 60             x.setnumber(telno);             return x.getnumber();         } else {             add(name, telno);         }     }     return null; }  //to complete public string removeentry(string name) {     return null; }  /**  * new printwriter object created, , loop used print  * name , number of each directoryentry console.  */ public void save() {     printwriter pw = null;     try {         pw = new printwriter(new filewriter("directory.txt", true));           (directoryentry x : thedirectory) {             pw.write(x.getname());             pw.write(x.getnumber());                        }     }     catch(exception e) {         e.printstacktrace();     }     {         pw.close();         } }     //private helper methods  private void reallocate() {     capacity = capacity * 2;     directoryentry[] newdirectory = new directoryentry[capacity];     system.arraycopy(thedirectory, 0, newdirectory,             0, thedirectory.length);      thedirectory = newdirectory; }  private void add(string name, string telno) {     if (size >= capacity) {         reallocate();     }     thedirectory[size] = new directoryentry(name, telno);     size = size + 1; }  private int find(string name) {     int = 0;     (directoryentry x : thedirectory) {         if (x.getname().equals(name)) {             return i;         }         i++;     }     return -1; }  @override public string format() {     return null; }    } 

arrayphonedirectorytester code:

public class arrayphonedirectorytester { public static void main (string[] args) {     //creates new phonedirectory     phonedirectory newdir = new arrayphonedirectory();      newdir.addchangeentry("joe perkins", "999999");  //line 17      system.out.println(newdir);       }  } 

a nullpointerexception thrown when try access object null, example call method on null object. in line 60:

x.getname().equals(name) 

you have 2 method calls. line throw npe, either 'x' null, or 'x.getname()' null. if refactor onto 2 separate lines, know which:

string xname = x.getname(); xname.equals(name) 

ideally should check if null:

string xname = x.getname(); if(xname == null) { } // error handling here xname.equals(name) ... 

edit

here example:

public string addchangeentry(string name, string telno) {  (directoryentry x : thedirectory) {     if (x.getname() == null) {         log.error("found user no name!!);         continue; // or throw exception if bad     }      if (x.getname().equals(name)) {              //line 60         x.setnumber(telno);         return x.getnumber();     } else {         add(name, telno);     } } return null;  } 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -