使用“if”检查后出现空指针异常陈述

发布于 2024-12-04 04:19:43 字数 5184 浏览 2 评论 0原文

我遇到了一个非常烦人的错误,说我遇到了空指针异常,但有一个 if 语句可以在继续之前检查文本是否为空:

    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!");
    }

}

它甚至考虑“else”的唯一一次是通过这个:

    if(Text.equals(null));

我我也尝试过这样做:

   if(Text != null));

这在过去对我有用,但现在不行!其他课程有:

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();
}

再次感谢!

错误:

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

当我输入或未输入某些内容并按下 Enter 按钮时,我会收到错误消息。

这是错误

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦纸 2024-12-11 04:19:43
 if(Text.equals(null));

每次 Text 为 null 时,上面都会抛出 NullPointerException。任何时候你使用“.”操作符 null 你会得到一个 NullPointerException。

如果您在 if(Text != null) 之后收到 NPE,请发布堆栈跟踪。

 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.

流年里的时光 2024-12-11 04:19:43

我的猜测是 textToAddRF 为空。

如果 textToAddJTextComponent (或子类,但我在这里猜测),然后是 getText() 方法不能返回 null。因此,Text 不能为 null,如本测试所示。

package test;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class TextComponentTest extends JFrame {
    JTextField tf;

    public TextComponentTest() {
        super();
        tf = new JTextField();
        getContentPane().add(tf);
    }

    public static void main(String[] args) {
        TextComponentTest test = new TextComponentTest();
        test.setVisible(true);

        String s = test.tf.getText();
        System.out.println(">" + s + "<");
        System.out.println(">" + s.length() + "<");

        test.tf.setText(null);
        s = test.tf.getText();
        System.out.println(">" + s + "<");
        System.out.println(">" + s.length() + "<");
    }
}

输出是:

><
>0<
><
>0<

My guess is either textToAdd or RF is null.

If textToAdd is a JTextComponent (or subclass, but I'm guessing here), then its getText() method CANNOT return null. Therefore, Text CANNOT be null, as this test shows.

package test;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class TextComponentTest extends JFrame {
    JTextField tf;

    public TextComponentTest() {
        super();
        tf = new JTextField();
        getContentPane().add(tf);
    }

    public static void main(String[] args) {
        TextComponentTest test = new TextComponentTest();
        test.setVisible(true);

        String s = test.tf.getText();
        System.out.println(">" + s + "<");
        System.out.println(">" + s.length() + "<");

        test.tf.setText(null);
        s = test.tf.getText();
        System.out.println(">" + s + "<");
        System.out.println(">" + s.length() + "<");
    }
}

Output is:

><
>0<
><
>0<
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文