Reputation: 23
I keep getting the Null Pointer Exception with my Class.
Client Class:
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int count = 0;
for (int i = 0; i < 100; i++){
count ++;
System.out.println(i);
Clip[] newClip = new Clip[100];
newClip[i] = new Clip();
menu();
int option = kb.nextInt();
switch(option){
case 1: System.exit(0);
break;
case 2: newClip[i].Input();
newClip[i].Output();
break;
case 3: int indexclip = 0;
int testclip = 0;
Scanner key = new Scanner(System.in);
System.out.println("Enter the index number of the clip: ");
testclip = key.nextInt();
for (int j = 0; j < 100; j++){
indexclip = newClip[j].getIndex(); // happens here
System.out.println(indexclip);
if(testclip == indexclip){
j = 120;
newClip[j].Output(); // and i would assume here too
}
}
break;
}
}
}
Clip Class:
import java.util.*;
public class Clip {
private int index;
private String surname;
private float length;
private float speed;
private String time = "testing";
public Clip(){
index = 0;
surname = "N/A";
length = (float) 0.00;
speed = (float) 0.00;
time = "0:00AM";
}
public void Output(){
System.out.println("Index: "+ index);
System.out.println("Surname: " + surname);
System.out.println("Length: " + length);
System.out.println("Speed: "+ speed + "m/s");
System.out.println("Time: "+ time);
}
public void Input(){
int testint;
float testfloat;
int spacePos;
String testString;
Scanner kb = new Scanner(System.in);
Scanner key = new Scanner(System.in);
System.out.println("Input an index number between 1 - 10000: ");
testint = kb.nextInt();
for (int i = 0; i < 100; i++){
if (testint < 1 || testint > 10000){
System.out.println("Input an index number between 1 - 10000: ");
testint = kb.nextInt();
}
else {
i = 120;
}
}
index = testint;
System.out.println("What is the competitor's Surname and their Given name: ");
surname = key.nextLine();
System.out.println("Length of the Tape in seconds: ");
testfloat = kb.nextFloat();
for (int i = 0; i < 100; i++){
if (testfloat < 1 || testfloat > 60){
System.out.println("Length of the Tape in seconds: ");
testfloat = kb.nextFloat();
}
else {
i = 120;
}
}
length = testfloat;
System.out.println("Estimated Speed of competitor in metres per second: ");
testfloat = kb.nextFloat();
for (int i = 0; i < 100; i++){
if (testfloat < 7 && testfloat > 13){
System.out.println("Estimated Speed of competitor in metres per second: ");
testfloat = kb.nextInt();
}
else {
i = 120;
}
}
speed = testfloat;
System.out.println("Time of recording between 0900 - 1700: ");
testString = key.nextLine();
for (int i = 0; i < 100; i++){
if (testString.length() != 4){
System.out.println("Time of recording between 09:00 - 17:00: ");
testString = key.nextLine();
}
else {
i = 120;
}
}
time = testString;
}
public int getIndex(){
return index;
}
public String getSurname(){
return surname;
}
public float getLength(){
return length;
}
public float getSpeed(){
return speed;
}
public String getTime(){
return time;
}
I understand that the Clip class has to be initialise and i have done that, as well as i do run option 2 first so that the constructor on the other class is not used. I just want it to read in an index number and scan the clip array for a matching index number
Any assistance would help big time
cheers
Upvotes: 0
Views: 108
Reputation: 19278
The NPE is here:
Clip[] newClip = new Clip[100];
You need to get that line out of your loop.
Upvotes: 0
Reputation: 3333
The problem is that you're creating the array every iteration in the loop, so that only the current entry (i) is not null
.
To fix it, move the line:
Clip[] newClip = new Clip[100];
needs to be outside the loop (before the for
statement).
Also, in the loop over int j
, this will only work if i == 100
. Not sure if that's guaranteed. Otherwise, the entries in newClip that are after i will also be null
Upvotes: 1
Reputation: 93842
You need to move the declaration of your array outside the first for loop.
Clip[] newClip = new Clip[100];
for (int i = 0; i < 100; i++){
/**/
}
Because when you're in the case 3 of your switch
statement, your looping through all the elements of the array Clip
(but since you have only set one element of the array, 99 elements of the array are actually set to null
).
Upvotes: 4