0

For my university assignment i'm trying to pass an int as a parameter to another class. However I keep receiving a null error for some reason. I even printed out the integer to see if it was null but it wasn't. Now I don't know what the problem could be. I've been trying different stuff for a couple hours now and I thought I should post my issue here.

I created a field in this class called currentBuild so that I can call methods in the Build class.

private Build currentBuild;

This is my method that gets the integer from the user and sends it to another class called Build.

private void addFromCat(){
    int partNo;

    System.out.println("Enter catalogue number of the part: ");
    partNo = In.nextInt();
    System.out.println(partNo);
    currentBuild.getNum(partNo);
}

This is the method in the Build class that receives the integer. It's called getNum.

public class Build
{

private List <Part> parts = new ArrayList <Part>();

/**
 * Constructor for objects of class Build
 */
public Build()
{

}

public void getNum(int partNo){

    int numberPart = partNo;
    System.out.println("part number is: " + numberPart);
    }



}
  • 3
    private Build currentBuild; should be private Build currentBuild = new Build();. The nullpointerexception wasn't the integers fault, you never assigned the currentBuild variable – Mano176 Sep 15 '19 at 16:43

1 Answers1

1
private Build currentBuild;

is a null attribute. It should be created.

private Build currentBuild = new Build();
Khaled Baklouti
  • 408
  • 2
  • 7