-1
public void addAccount(int tcNo, int musteriNo, String name, String surname) {
    System.out.println("Choose an account type:");
    System.out.println("1. Deposit Acc");
    System.out.println("2. Normal Acc");
    System.out.println("3. Saving Acc");

    Scanner getInput = new Scanner(System.in);

    int accountType = getInput.nextInt();

    getInput.close();

    String hesapTuru = hesaplar.get(0).get_hesapTuru();

    BankaHesap newAcc;

    switch (accountType) {
        case 1: // Vadeli hesabının açılması
            System.out.println("Vadeli hesap için başlangıç miktarını giriniz:");
            double vadeliBakiye = getInput.nextDouble();    /* Vadeli hesabın başlangıç miktarı kullanıcıdan alınır. */

            switch (hesapTuru) {
                case "Maaş":
                    newAcc = new VadeliHesap(hesapTuru, vadeliBakiye, 0.20f);
                    break;
                case "Normal":
                    newAcc = new VadeliHesap(hesapTuru, vadeliBakiye, 0.10f);
                    break;
            }

            hesaplar.add(newAcc);
            break;
        case 2: // Vadesiz hesabının açılması
            System.out.println("Vadesiz hesap için başlangıç miktarını giriniz:");
            double vadesizBakiye = getInput.nextDouble();    /* Vadesiz hesabın başlangıç miktarı kullanıcıdan alınır. */

            switch (hesapTuru) {
                case "Maaş":
                    newAcc = new VadesizHesap(hesapTuru, vadesizBakiye, 0);
                    break;
                case "Normal":
                    newAcc  = new VadesizHesap(hesapTuru, vadesizBakiye, 8);
                    break;
            }

            hesaplar.add(newAcc);
            break;
        case 3: // Yatırım hesabının açılması
            System.out.println("Yatırım hesabı için başlangıç miktarını giriniz:");
            double yatirimBakiye = getInput.nextDouble();    /* Yatırım hesabının başlangıç miktarı kullanıcıdan alınır. */

            System.out.println("Niçin yatırım yapacağınızı giriniz:");
            String yatirimTuru = getInput.next();   /* Yatırım hesabının türü kullanıcıdan alınır. */

            newAcc = new YatirimHesabi(hesapTuru, yatirimBakiye, yatirimTuru, 1); /* Yatırım hesabı açılır. */
            hesaplar.add(newAcc);
            break;
        default:
            System.out.println("Hatalı seçim yaptınız.");
    }
}

I'm getting "The local variable newAcc may not have been initialized" error. What should I do for it?

It's a banking app (java). This code block is in Customer class in Customer.java file.

zforgo
  • 2,508
  • 2
  • 14
  • 22
Aytgg
  • 1
  • 1
    initialize it with : BankaHesap newAcc = null; – avihaiB Apr 30 '23 at 10:47
  • Does this answer your question? [Variable might not have been initialized error](https://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) – Progman Apr 30 '23 at 12:34

2 Answers2

0

In your inner switch statement you must add a default clause which either throws an exception or initializes newAcc with some value so that there is no execution path that will lead to newAcc being uninitialized at the end of the switch statement.

So, it could be:

default:
    throw new IllegalArgumentException( "hesapTuru was neither 'Maaş' nor 'Normal'" );

or:

default:
    newAcc = null;
    break;
}
if( newAcc != null )
    hesaplar.add(newAcc);
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
0

As Mike Nakis suggested, you can add a statement that initializes newAcc in the default case of the switch statement. Alternatively, you can initialize the newAcc variable with a default value of null when you declare it: BankaHesap newAcc = null;

However, initializing newAcc to null can lead to a null pointer exception (NPE) if you forget to assign a value to it later. By handling the default case explicitly and assigning a value to newAcc, you can better manage the logic of your application and avoid unexpected errors or behavior. Therefore, Mike's solution of handling the default case explicitly is better because it avoids the possibility of a null pointer exception and gives you more control over your application logic.

Nurma
  • 26
  • 1
  • 3