gpt4 book ai didi

在我的计算机上运行代码但不在我的 friend 上运行代码时出现 java.lang.NullPointerException 错误

转载 作者:行者123 更新时间:2023-12-01 23:25:25 25 4
gpt4 key购买 nike

您好,当尝试运行我的代码时,我不断收到 java.lang.NullPointerException 错误,但我确实知道它在 friend 计算机上工作不应该有任何错误。我正在使用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();
}

}

最佳答案

您在哪里定义命令列表?你有一个声明,但据我所知,你并没有创造任何东西。

public static List< ICommand > commands;


commands = new List< .... ?

我可以看到您定义了命令本身,但没有定义列表。

关于在我的计算机上运行代码但不在我的 friend 上运行代码时出现 java.lang.NullPointerException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20083891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com