0

Here's my writeToFile() method:

public void writeToFile(String string){
    x.format("%s", string);
}

And this is how I use this method:

private class HandlerClass implements ActionListener{
    public void actionPerformed(ActionEvent event){
        string = tekstPole.getText();
        tekstLabel.setText(string);
        f.writeToFile(string);
    }
}

When I try to compile I get

Exception in thread "main" java.lang.NullPointerException at Files.readFile(Files.java:30) at Bicycle.(Bicycle.java:34) at Hristijan.main(Hristijan.java:10)

What could be the problem?

user3330053
  • 173
  • 1
  • 16
  • 2
    Provide the code at `Bycicle.java:34` (which is where the variable with `null` value is). – Luiggi Mendoza Nov 20 '14 at 17:40
  • 2
    You need to learn the general concepts of how to debug a NPE (NullPointerException). **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Nov 20 '14 at 17:40
  • 2
    _When I try to compile_ No. An **Exception** is something you get at runtime. – Sotirios Delimanolis Nov 20 '14 at 17:40
  • @user3330053 what is the specific line 30 of Files.java? It seems you are trying to invoke a method on a null instance of a class. You have to figure out which instance is null and fix that. – La-comadreja Nov 20 '14 at 17:42

0 Answers0