2017-03-20 7 views
1

私のプログラムを提案に従って編集した後、プログラムの実行中に表示メッセージが表示されません。 AccountBalance.javaがコンパイルされ、AccountBalance.classファイルが生成されていますが、プログラムが実行されているときに何も表示されていないカーソルが点滅しています。私のプログラムが実行されておらず、 "スレッド内の例外"メイン "java.lang.NullPointerException"をスローする

class Balance { 
 
    private Scanner bank; 
 
    public int userAccount; 
 
    public int bankAccountNumber; 
 

 
    void account() 
 
    { bank = new Scanner(System.in); 
 
     int openingAmount = bank.nextInt(); 
 
     System.out.print("Please deposit an amount more than Rs. 1000.00 to open a Bank account: " + openingAmount); 
 

 
     if (openingAmount > 1000) { 
 
      System.out.println("Your Bank account has opened successfully"); 
 
      userAccount = openingAmount; 
 

 
      System.out.println("Enter your Bank account number : "); 
 
      bank = new Scanner(System.in); 
 
      bankAccountNumber = bank.nextInt(); 
 
     } else{ System.out.println("Bank account not opened.\nDo you want to open a bank account then..!!"); 
 
      this.account(); //Ask again for opening an account 
 
     } 
 
    } 
 
    void withdrawal() { 
 
     bank = new Scanner(System.in); 
 
     int w = bank.nextInt(); 
 
     int b = userAccount - w; 
 
     System.out.println("Withdrawal Amount is : " + w ); 
 
     if (w < 100) 
 
     { 
 
      System.out.println("Unable to process your request"); 
 
     } else { 
 
      System.out.println("Your Balance Amount is : " + b); 
 
     } 
 
    } 
 

 
    void deposit() { 
 
     bank = new Scanner(System.in); 
 
     int d = bank.nextInt(); 
 
     System.out.println("Deposited amount is : "); 
 
     userAccount += d; 
 
     System.out.println("Your Balance Amount is : " + userAccount); 
 
    } 
 
} 
 

 
public class AccountBalance { 
 
    public static void main(String[] args) { 
 
     Balance s = new Balance(); 
 
     s.account(); 
 
     s.withdrawal(); 
 
     s.deposit(); 
 
     System.out.println(" Current account balance is : "+s.userAccount); 
 
    } 
 
}

私は、私は次のエラーを取得していますプログラムを実行しようとしています:

"例外をスレッドで "メイン" のjava.lang.NullPointerException"

I 2つのクラスのうち1つがmainメソッドを持つプログラムを作成しようとしています。銀行口座開設の最低金額はRsです。 1000、口座開設金額、銀行口座番号の入出金はユーザーが入力します。現在のアカウント残高を表示する必要があり、アカウントを継続するかどうかを選択するユーザーがオプションを選択して終了すると、プログラムが終了するかどうかを確認する必要があります。私が間違ってやっていること..それに対抗して解決する方法...?

import java.util.Scanner; 
class Balance 
{ 
    private Scanner bank; 
    public int userAccount; 
    public int bankAccountNumber; 
    public int balance; 

    public void account() 
    { 
     balance = bank.nextInt(); 
     System.out.print("Please deposit an amount of minimum Rs. 1000.00 to open a Bank account : "); 

     if (balance >= 1000) 
     { 
      System.out.println("Account opened successfully"); 
      bankAccountNumber = bank.nextInt(); 
      System.out.println("Enter your 4 digit Bank account number : "); 
     } else { 
      System.out.println("Bank account not opened.\nDo you want to open a bank account then..!!"); 
      this.account(); //Ask again for opening an account 
     } 
    } 
    void withdrawal() { 
     int w = bank.nextInt(); 
     int b = userAccount - w; 
     System.out.println("Withdrawal Amount is : " + w ); 
     if (w < 100) 
     { 
      System.out.println("Unable to process your request"); 
     } else { 
      System.out.println("Your Balance Amount is : " + b); 
     } 
    } 

    void deposit() { 
     int d = bank.nextInt(); 
     System.out.println("Deposited amount is : "); 
     userAccount += d; 
     System.out.println("Your Balance Amount is : " + userAccount); 
    } 
} 

public class AccountBalance { 
    public static void main(String[] args) { 
     Balance s = new Balance(); 
     s.account(); 
     s.withdrawal(); 
     s.deposit(); 
     System.out.println("Your current balance is : " + s.userAccount); 
    } 
} 
+0

どのラインでエラーが発生していますか? –

+1

あなたのスキャナは初期化されていないはずです –

答えて

0

それはNullPointerExceptionを作成しているので、それが初期化されていない、以来、account方法の初めにbalance = bank.nextInt();を呼び出す前bank = new Scanner(System.in);を追加してみてください。

1

クラスのコンストラクタを作成し、スキャナのバランスをとり、スキャナを初期化します。

private Scanner bank; 

スキャナは定義されていますが、初期化されていません。 このコンストラクタをBalanceクラスに追加します。

public Balance() 
{ 
    bank = new Scanner(System.in); 
} 
関連する問題