0

enter image description hereI have written a file writer and a file reader class in which you put data into and use it by file reader in another class methods. The type of my variable defined in Account class is integer.To write it in file, I change the type as String(because I have my other variables as String)...when I want to use that variable in my method in Account class, I can not compare it with the local variable(which is filled by user).It gives me a "NullPointerException" error. Can anyone help me with this?(The code is just a summary)enter image description here

public class Account {
    int PIN;
public void meth_name(){
    int pin;

    if(PIN == pin)


    }
}

public class filewriter {

    public void met_name(){
        ....
        fw.write("PIN: "+ Integer.toString(PIN));
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Rojin
  • 1,045
  • 9
  • 15
  • Please don't add things like `[SOLVED]` to a question title. Instead accept the answer that helped you (or if the question is not answered (but not on hold), post your own answer). – Mark Rotteveel Mar 31 '19 at 16:43

1 Answers1

0

In Java when you are working with primitive types you cannot get NullPointerException because they are not pointers, they are allocated in Stack Memory.
So you are getting NullPointerException because some other variable is null. It is difficult to understand which variable is null from the code snipped you provided.
In your case NullPointerException might be thrown because

  • The Account object which PIN you are acquiring is null
  • The fw(filewriter) object which you are using is not yet initialized

Please provide more code or the stack trace of NullPointerException to understand exception's root cause. Also please tell what is the type of fw object you used in the code. Is it java.io.PrintWriter or your custom class?

Rub
  • 160
  • 2
  • 12
  • I added photos... – Rojin Mar 31 '19 at 12:55
  • I assume the array `accounts` is null or there is such `i` for which `accounts[i]` is null. Add null-checks and test it. Also provide the exception Stack Trace. – Rub Mar 31 '19 at 13:23
  • I'm happy that I could help you :D ... Could you please mark the answer as correct one if it really helped you? – Rub Mar 31 '19 at 15:42