0

I'm new to java ,and I get this error on compiler

Enter name : pot Exception in thread "main" java.lang.NullPointerException at account.BankTest.main(BankTest.java:17) C:\Users\Carl the INVOKER\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 7 seconds)

Class Account

package account;

public class Account {
    private String name;
    private String surname;
    private int age;
    private int sex;

    public Account(String name, String surname, int age, int sex) {
        this.name = name;
        this.surname = surname;
        this.age = age;
        this.sex = sex;
    }

    public Account() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;

    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;

    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }
}

Class BankTest

package account;

import java.util.Scanner;

public class BankTest {

    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);

        Account user[] = new Account[10];

        System.out.println("Enter name : ");
        user[1].setName(scan.nextLine());

        System.out.println(user[1].getName());
    }
}
4
  • Has this thread been resolved? It would help a lot for other users and myself if you marked a solved problems. If you have found a better solution, please share with the community. Commented Apr 26, 2017 at 4:18
  • I didn't use object arrays , i use ArrayLists. The code which is your suggested didn't work. Commented Apr 26, 2017 at 20:27
  • This is an object array Account user[] = new Account[10]; not an ArrayList. An ArrayList would be something like this List<Account> user = new ArrayList<Account>(); Commented Apr 26, 2017 at 20:30
  • Either way, just do this as proof that you did not initialize the elements of your array. Put System.out.println(user); in your code before your scanner reads nextLine you'll see that you have an array of null. Commented Apr 26, 2017 at 20:32

1 Answer 1

0

Your array has no values in it. Account user[] = new Account[10]; only creates the array, but doesn't initialize the elements in the array. Therefore, when you execute this statement user[1].setName(scan.nextLine());, user[1] doesn't reference an Account object, it actually references a null value; thus, calling the method setName will throw a NullPointerException. To initialize your account array, you need to do something like this before accessing it.

for (int i=0: i<user.length; i++) {
    user[i] = new Account(null, null, 0, 0);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.