NullPointException

Discussion in 'Plugin Development' started by RightLegRed, Jan 22, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Archelaus

    While trying to read a file and write it to an array I'm getting a NullPointException. I understand why I'm getting it, but I can't seem to get my head around it.

    Could someone tell me how to solve it? I've marked the line I'm getting it from.

    Code:
    public String[] reader() throws IOException {
            String[] lines = {"AT LEAST 1 VALUE!"};
                FileReader readFile = null;
                BufferedReader readBuffer;
                String line;
                readFile = new FileReader("warps.txt");
                    readBuffer = new BufferedReader(readFile);
                    line="";
                    while(line != null){
                        line = readBuffer.readLine();
                    System.out.println(line);
    
                    lines[lines.length + 1] = line.toString(); //- Here
    
                    }
                    readBuffer.close();
                    return lines;
        }
    Please excuse the terrible programming, I've been trying to get it to work and I've thrown all my standards out the window so I can at least get it to.
     
  2. Offline

    Raphfrk

    Java arrays cannot be resized. You have to decide how many elements at run time.

    What you probably want is an ArrayList;
    --- merged: Jan 22, 2011 10:52 AM ---
    Code:
    ArrayList<String> listLines = new ArrayList<String>();
    
    listLines.add("test");
    listLines.add("test2");
    String[] lines = listLines.toArray(new String[] {});
    
    --- merged: Jan 22, 2011 10:52 AM ---
    The toArray method is weird, you have to pass it a blank string array to get it to generate a string array.
     
  3. Offline

    Novar234

    Or if you want arrays... you can copy array to the temp then make a new array of the new size you want then copy the data from temp to the new array >.<

    :p
     
  4. Offline

    Archelaus

    Thanks, I got that working. But it now breaks when doing this:

    Code:
        public Boolean nameExists(String Name) throws IOException  {
            Boolean tf = false;
            for(String s : reader()) {
                String[] splitS = s.split(":"); //Right Here
                if(splitS[0] == Name) {
                    tf = true;
                }
            }
            return tf;
        }

    Very sorry about the ignorance.

    Whoops, wrong line. Still doesn't work though same error.
     
  5. Offline

    blaatz0r

    Ah, the good old comparing by reference instead of comparing by value :)

    If you use '==' to compare in Java, it will compare objects in your memory instead of comparing the values of the strings. splitS[0] is a different object in memory than Name, so it will break.

    This is what you want:

    Code:
    if (splits[0].equals(Name)) {
         tf = true;
    }
    or a bit more compact (the equals function returns a boolean, and the variable tf is of type boolean, so why not assign it immediately?):

    Code:
    tf = splits[0].equals(Name);
    
     
  6. Offline

    Archelaus

    Thanks, I actually come from a Lua backgroud (Arrays can be resized and you can use == to check two values) so everything is new to me :)
    --- merged: Jan 22, 2011 11:54 AM ---
    OK, Sorry for this. But that only solved half the problem. Now, it's giving me the exact same error on here:

    Code:
        public Boolean nameExists(String Name) throws IOException  {
            Boolean tf = false;
            for(String s : reader()) {
                String[] splitS = s.split(":");  //Right Here ---
                tf = splitS[0].equals(Name);
            }
            return tf;
        }
    
     
  7. Offline

    blaatz0r

    Maybe reader() returns null? I'm not sure at this point.

    If you do

    Code:
    "".split(":");
    It still should return an empty string, so empty lines should not cause a nullpointer.
     
  8. Offline

    Archelaus

    It was returning Null on the last one, I added a null check.

    Works now, thanks a lot :)
     
Thread Status:
Not open for further replies.

Share This Page