Null Pointer Exception after checking with 'if' statement

36,558
 if(Text.equals(null));

The above will throw a NullPointerException each time Text is null. Anytime you use the "." operator on null you get a NullPointerException.

If you are getting a NPE after if(Text != null), please post the stack trace.

Share:
36,558
liloka
Author by

liloka

I'm a graduate with a first in Computer Science, now a grad dev at ThoughtWorks. I love programming, Human Computer Interaction, dancing, travelling and photography. I'm the treasurer for the Manchester Metropolitan Computing Society, and do a number of talks involving testing, Git, and LaTeX. I'm also a StemNET ambassador, and will be running my first {Code Club} next term.

Updated on July 09, 2022

Comments

  • liloka
    liloka almost 2 years

    I'm getting a really annoying error, saying I'm getting a null pointer exception but there's an if statement to check to see if the text is null before proceeding:

        public String[] getFileData() throws IOException
    {
        String file_name = "C:/Users/Liloka/Source/textfiles/Lines.txt";
    
        try {
            ReadFile file = new ReadFile(file_name);
            aryLines = file.OpenFile();
    
            for(int i =0; i<aryLines.length; i++)
            {
                System.out.println(aryLines[i]);
            }
        }
    
        catch(IOException e)
        {   
            System.out.println(e.getMessage());
        }
        return aryLines;
    }
    
    public void actionPerformed(ActionEvent evt)
    {
        if(evt.getSource() == enterBtn)
        {
            String Text = textToAdd.getText();
            if(!(Text.equals(null)))
            {
                RF.addNewElement(Text);
                System.out.println(Text);
    
                try
                {
                    RF.writeToFile();
                    getFileData();
                }
            catch(Exception e)
                {
    
                }
            }
            else    JOptionPane.showMessageDialog(null, "Please enter a word!");
        }
    
    }
    

    The only time it even considered the 'else' was through this:

        if(Text.equals(null));
    

    I've also tried doing:

       if(Text != null));
    

    which has worked for me in the past but not now! Other classes are:

    public String[] OpenFile() throws IOException
    {
        FileReader fr = new FileReader(path);
        BufferedReader br = new BufferedReader(fr);
    
        int numberOfLines = readLines();
        textData = new String[numberOfLines];
        int i;
            for(i=0; i<numberOfLines; i++)
            {
                    textData[i] = br.readLine();
            }
    
        br.close();
        return textData;
    }
    
    int readLines() throws IOException
    {
        FileReader file_to_read = new FileReader(path);
        BufferedReader bf = new BufferedReader(file_to_read);
    
        String aLine;
        numberOfLines=0;
    
        while((aLine = bf.readLine()) != null)
        {
            numberOfLines++;
        }
        //numberOfLines++;
        bf.close();
        return numberOfLines;
    }
    
    public void addNewElement(String newElement)
    {   
        String texticles = newElement;
        numberOfLines = numberOfLines++;
        textData[numberOfLines] = texticles;
        //numberOfLines++; //Increments numberOfLines for the next element to be added
    }
    
    public void writeToFile() throws IOException
    {
        FileWriter fstream = new FileWriter(path);
        BufferedWriter outFile = new BufferedWriter(fstream);
        //numberOfLines++;
    
            outFile.write(textData[numberOfLines]);
            //outFile.write(",");
    
            outFile.write("\r\n");
    
        outFile.close();
    }
    

    Thank you, again!

    Error:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at textfiles.JListExample.actionPerformed(JListExample.java:115)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    

    I get the error when I've typed or not typed something and pressed the enter button.

    This is the error for

    if(Text != null)
    
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at textfiles.JListExample.actionPerformed(JListExample.java:115)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)