-1

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

ファイルが存在するときはいつでも私のプログラムは正常に実行されますが、検索結果が false にjava.lang.NullPointerExceptionなると...

助けてください、どうすればいいですか

エラー:

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;
}
}
4

1 に答える 1

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;
            }
        }
    }
    
于 2013-10-01T07:10:46.233 に答える