检查“if”语句后出现java空指针异常
我得到了一个非常恼人的错误,说我得到了一个空指针异常,但是在继续之前有一个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!");
}
}
它唯一一次考虑“其他”是通过以下方式:
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)
# 1 楼答案
我的猜测是
textToAdd
或RF
为空如果} (或者子类,但我在这里猜测),那么它的^{} 方法不能返回null。因此,
textToAdd
是一个^{Text
不能为null,如本测试所示输出为:
# 2 楼答案
每当Text为空时,上述内容将抛出NullPointerException。任何时候你都可以使用“.”运算符在null上,您将得到一个NullPointerException
如果您在(Text!=null)之后获得NPE,请发布堆栈跟踪