您好我在尝试运行我的代码时不断收到java.lang.NullPointerException错误,但是我知道在朋友计算机上它不应该有任何错误。我正在使用Eclipse,似乎无法找到问题所在。关于我做错了什么的指针,或许有设置?谢谢
import java.util.List;
import java.util.Scanner;
public class Bank {
public static List< Account > accounts;
public static List< ICommand > commands;
public static void main( String[] args ) {
commands.add( new CreateNewCommand() );
commands.add( new OpenAccountCommand() );
commands.add( new MakePaymentCommand() );
commands.add( new IncrementMonthCommand() );
Scanner input = new Scanner( System.in );
boolean run = true;
while ( run ) {
System.out.println( "Commands:" );
for ( ICommand command : commands ) {
if ( command instanceof CreateNewCommand )
System.out.println( ( ( CreateNewCommand )command ).command );
if ( command instanceof OpenAccountCommand )
System.out.println( ( ( OpenAccountCommand )command ).command );
if ( command instanceof MakePaymentCommand )
System.out.println( ( ( MakePaymentCommand )command ).command );
if ( command instanceof IncrementMonthCommand )
System.out.println( ( ( IncrementMonthCommand )command ).command );
}
// Close account is the same as open account, so no extra classes needed.
System.out.println( "CloseAccount" );
System.out.println();
String line = input.nextLine();
String[] inputArgs = line.split( "\\s+" );
switch ( inputArgs[ 0 ] ) {
case "CreateNew":
CreateNewStruct structData0 = new CreateNewStruct();
structData0.name = inputArgs[ 1 ];
structData0.address = inputArgs[ 2 ];
structData0.phone = inputArgs[ 3 ];
structData0.ssn = inputArgs[ 4 ];
structData0.age = inputArgs[ 5 ];
structData0.initialBalance = Integer.parseInt( inputArgs[ 6 ] );
structData0.loanLen = Integer.parseInt( inputArgs[ 7 ] );
structData0.credit = Integer.parseInt( inputArgs[ 8 ] );
structData0 = ( CreateNewStruct ) ( ( CreateNewCommand ) commands.get( 0 ) ).DoCommand( ( Object ) structData0 );
accounts.add( structData0.account );
System.out.println( "Account #" + structData0.account.acntNum + " was created." );
break;
case "OpenAccount":
OpenAccountStruct structData1 = new OpenAccountStruct();
structData1.acntNum = Integer.parseInt( inputArgs[ 1 ] );
structData1.accounts = accounts;
structData1 = ( OpenAccountStruct ) ( ( OpenAccountCommand ) commands.get( 1 ) ).DoCommand( ( Object ) structData1 );
if ( structData1 == null ) {
System.out.println( "Could not find account #" + Integer.parseInt( inputArgs[ 1 ] ) + "." );
break;
}
Account account1 = structData1.account;
System.out.println( "Account Number: " + account1.acntNum );
System.out.println( "Name: " + account1.name );
System.out.println( "Address: " + account1.address );
System.out.println( "Phone: " + account1.phone );
System.out.println( "SSN: " + account1.ssn );
System.out.println( "Age: " + account1.age );
System.out.println( "Initial Balance: " + account1.initialBalance );
System.out.println( "Loan Length: " + account1.loanLen );
System.out.println( "Credit: " + account1.credit );
System.out.println( "Monthly Payment: " + account1.monthlyPayment );
System.out.println( "Interest Rate: " + account1.interestRate );
System.out.println( "Balance: " + account1.balance );
break;
case "MakePayment":
MakePaymentStruct structData2 = new MakePaymentStruct();
structData2.acntNum = Integer.parseInt( inputArgs[ 1 ] );
structData2.payment = Double.parseDouble( inputArgs[ 2 ] );
structData2.accounts = accounts;
structData2 = ( MakePaymentStruct ) ( ( MakePaymentCommand ) commands.get( 2 ) ).DoCommand( ( Object ) structData2 );
if ( structData2 == null ) {
System.out.println( "Could not find account #" + Integer.parseInt( inputArgs[ 1 ] ) + "." );
break;
}
Account account2 = structData2.account;
System.out.println( "New balance for account #" + account2.acntNum + " is " + account2.balance );
break;
case "IncrementMonth":
IncrementMonthStruct structData3 = new IncrementMonthStruct();
structData3.acntNum = Integer.parseInt( inputArgs[ 1 ] );
structData3.accounts = accounts;
structData3 = ( IncrementMonthStruct ) ( ( IncrementMonthCommand ) commands.get( 3 ) ).DoCommand( ( Object ) structData3 );
if ( structData3 == null ) {
System.out.println( "Could not find account #" + Integer.parseInt( inputArgs[ 1 ] ) + "." );
break;
}
Account account3 = structData3.account;
System.out.println( "Added the monthly interest for account #" + account3.acntNum );
break;
case "CloseAccount":
OpenAccountStruct structData4 = new OpenAccountStruct();
structData4.acntNum = Integer.parseInt( inputArgs[ 1 ] );
structData4.accounts = accounts;
structData4 = ( OpenAccountStruct ) ( ( OpenAccountCommand ) commands.get( 1 ) ).DoCommand( ( Object ) structData4 );
if ( structData4 == null ) {
System.out.println( "Could not find account #" + Integer.parseInt( inputArgs[ 1 ] ) + "." );
break;
}
Account account4 = structData4.account;
if ( account4.balance > 0.00d ) {
System.out.println( "There is still money that needs to be payed!" );
break;
}
accounts.remove( account4 );
break;
default:
run = false;
break;
}
System.out.println();
}
input.close();
}
}
答案 0 :(得分:2)
您在哪里定义commands
列表?你有一份宣言,但你没有创造任何我能看到的东西。
public static List< ICommand > commands;
commands = new List< .... ?
我可以看到你自己定义命令,但不是列表。
答案 1 :(得分:1)
该行
public static List< ICommand > commands;
仅声明List
在某些时候,您需要像这样初始化commands
:
commands = new ArrayList< ICommand >();
可以使用main
方法完成此操作:
public static void main( String[] args ) {
commands = new ArrayList< ICommand >();
commands.add( new CreateNewCommand() );
或宣布commands
时:
public static List< Account > accounts;
public static List< ICommand > commands = new ArrayList< ICommand >();
public static void main( String[] args ) {
您还需要初始化accounts
。您可以像初始化commands
一样执行此操作。
您所拥有的代码不应该在任何计算机上运行。您确定在两台计算机上运行完全相同的代码吗?