Home java What causes the NullPointerException error

What causes the NullPointerException error




I have been studying Java relatively recently (I have experience in writing applications in C++ OpenGL GLSL). I decided to write a bot, like a question-answer, I sketched the logic, compiled – an error appears java.lang.NullPointerException

Here is the execution log:

Exception in thread "main" java.lang.NullPointerException
  at iostream.Main.main (Main.java:16)
/home/hays/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned:
ASSEMBLY COMPLETE FAILED (total time: 0 seconds)

Here are the sources, a dictionary with keys and values ​​is read from a file and parsed into HashMap data.txt

How are you ?! | How are you?!
Not bad either | I’m glad!
What is your name? | Holo

The Main class:

package iostream;
import java.io.IOException;
import java.util.Scanner;
public class Main {
  private static IOStream StreamIO;
  private static Parser Holo;
  public static void main (String [] args) throws IOException {
    String patch = "data.txt";
    StreamIO = new IOStream (patch);
    String Text = StreamIO.read ();
    System.out.println (Text);
    System.out.println (StreamIO.NumOFphrases ());
    Holo.ParseString (Text, StreamIO.NumOFphrases ());
    Scanner in = new Scanner (System.in);
    String question = in.nextLine ();
    System.out.println (Holo.DiologHolo.get (question));

Parser class:

package iostream;
import java.util.HashMap;
import java.util.Map;
public class Parser {
public Map DiologHolo = new HashMap & lt; String, String & gt; ();
  public boolean SetKeyValue (String Key, String Value)
    if (Key.isEmpty () & amp; & amp; Value.isEmpty ())
      return false;
      DiologHolo.put (Key, Value);
    return true;
  public void ParseString (String Text, int NumOFphrases)
    String KeyValue [] = Text.split ("|"); // by space
    for (int i = 0; i & lt; NumOFphrases; i + = 2) {
      System.out.println (KeyValue [i]);
      System.out.println (KeyValue [i ++]);
      SetKeyValue (KeyValue [i], KeyValue [i ++]);

IOStream Class:

package iostream;
import java.io. *;
public class IOStream {
  // class for reading the file
  private InputStream inputstream;
  // class for writing to file
  private OutputStream outputStream;
  // path to the file that we will read and write
  private String path;
  private int Numofphrases = 0;
  public int NumOFphrases () {
  return this.Numofphrases;
  public IOStream (String path) {
    this.path = path;
  // read file using InputStream
  public String read () throws IOException {
    // initialize the stream for reading
    inputstream = new FileInputStream (path);
    int byteChar;
    ByteArrayOutputStream baos = new ByteArrayOutputStream ();
    while ((byteChar = inputstream.read ())! = - 1)
      baos.write (byteChar);
      if ('|' == (char) byteChar)
        Numofphrases ++;
    // close the stream
    inputstream.close ();
    return baos.toString ();
// write to file using OutputStream
  public void write (String st) throws IOException {
    // initialize the stream to output data
    // which will allow us to write new data to the file
    outputStream = new FileOutputStream (path);
    // pass the resulting string to st and convert it to a byte array.
    outputStream.write (st.getBytes ());
    // close the output stream
    // only after we close the stream, the data will go to the file.
    outputStream.close ();

Answer 1, authority 100%

You have not initialized Holo

private static Parser Holo;

and trying to use its method

Holo.ParseString (Text, StreamIO.NumOFphrases ());

You need to initialize the object like this

private static Parser Holo = new Parser ();

Answer 2, authority 98%

Java outputs:


in cases when a reference to a “null” object occurs.

Run your code in debug, find where you have null, add a check if this is normal behavior, or find the reason why the object you are accessing is null.

I also recommend reading this article, it is devoted to this problem and is written just for beginners.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions