遇到java.lang.NullPointerException错误,请帮帮我

时间:2013-10-01 07:00:34

标签: oop nullpointerexception

enter code here我有一个私有布尔exist=false;,这是我payrool class

的局部变量

我还有一个方法searchRecord(int payrollPeriod, int empNo)

public void searchRecord(int payrollPeriod, int empNo)
    {
        for(int x=0;x<100;x++)
        {
            if(trans[x].getPayrollPeriod() == payrollPeriod && trans[x].getEmpNo() == empNo)
            {
                payrollCounter=x;
                exist=true;
                break;
            }
        }
    }

我的数组trans[]Transaction类型(我使用的合成),包含payrollPeriodempNo的记录。

我的程序在文件存在时运行良好,但是当搜索结果为假java.lang.NullPointerException时...

请帮助我,我该怎么办

ERROR:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at payroll.Payroll.searchRecord(Payroll.java:350)
at payroll.Payroll.actionPerformed(Payroll.java:184)
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$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$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)

交易类:

package payroll;

public class Transaction extends Employee
{
//local variable
private int payrollPeriod;
private double hoursWorked;

//constructor
public Transaction(int payrollPeriod, int empNo, String name, String department, double payRate, double hoursWorked)
{
    super(empNo, name, department, payRate);
    this.payrollPeriod=payrollPeriod;
    this.hoursWorked=hoursWorked;
}

//methods
public int getPayrollPeriod()
{
    return payrollPeriod;
}
public double getHoursWorked()
{
    return hoursWorked;
}
}

1 个答案:

答案 0 :(得分:1)

数组trans[x]包含null个值。

你应该:

  1. 构建数组时不允许数组中的null值。
  2. 检查循环中的null值并跳过/处理它们。

    public void searchRecord(int payrollPeriod, int empNo) {
        for(int x = 0; x < trans.length; x++) {
            if (trans[x] == null) {
                System.out.println("Skipped null value in trans[x]");
                continue;
            }
    
            if (trans[x].getPayrollPeriod() == payrollPeriod && trans[x].getEmpNo() == empNo) {
                payrollCounter = x;
                exist = true;
                break;
            }
        }
    }