Reputation: 1
it gets complied and also works for 2 steps after that gives null pointer exception. Q;-> how to allocate memory to multi-dimensional array object on line 48 using vectors or without them ??
import java.util.Scanner;
class cpa
{
Scanner in=new Scanner(System.in);
int classes,sec[],n[][];
String name[][][];
float sub1[][][],sub2[][][],per[][][],sectionavg[][],clasavg[],schoolavg;
void start()
{
int choice;
System.out.println("Press [1] for creating record\tPress [2] for viewing result\n\t\tPress [3] to exit");
choice=in.nextInt();
if(choice==1)
{
create();
}
else if(choice==2)
{
view();
}
else if(choice==3)
{
System.out.println("HAVE A NICE TIME...");
}
else
{
System.out.println("INVALID INPUT");
start();
}
}
void create()
{
System.out.println("Enter total classes : ");
classes=in.nextInt();
clasavg=new float[classes];
for(int i=1;i<=classes;i++)
{
clasavg[i]=0;
System.out.println("HOW MANY SECTIONS FOR "+i+" CLASS");
sec[i]=in.nextInt();
for(int j=1;j<=sec[i];j++)
{
sectionavg[i][j]=0;
System.out.println("ENTER NO OF STUDENT FOR SECTION "+j+" :");
n[i][j]=in.nextInt();
cpa shivalik[][][]=new cpa[classes][sec[i]][n[i][j]];//multi-dimensional OBJECT
for(int k=1;k<=n[i][j];k++)
{
shivalik[i][j][k]= new cpa();
shivalik[i][j][k].getinfo(i,j,k);
sectionavg[i][j]+=shivalik[i][j][k].per(i,j,k);
}
clasavg[i]+=sectionavg[i][j];
}
schoolavg+=clasavg[i];
}
start();
}
float sectionavg(int i,int j){return sectionavg[i][j]/n[i][j];}
float clasavg(int i){return clasavg[i]/sec[i];}
float schoolavg(){return schoolavg/classes;}
void view()
{
int choice,clasnum,secnum,roll;
System.out.println("[1]-> SCHOOL PERFORMANCE\t[2]-> CLASS PERFORMANCE\n[3]SECTION PERFORMANCE\t[4]STUDENT PERFORMANCE\n\t\t[5]EXIT");
choice=in.nextInt();
switch(choice)
{
case 1:System.out.println("YOUR SCHOOL PERFORMANCE ACCORDING TO RECORDS IS "+schoolavg()+" %");view();
case 2:System.out.println("ENTER THE CLASS NUMBER WHOSE PERFORMANCE YOU WANT TO VIEW :");
clasnum=in.nextInt();
System.out.println(clasnum+"CLASS PERFORMANCE ACCORDING TO RECORDS IS "+clasavg(clasnum)+" %");view();
case 3:System.out.println("ENTER THE CLASS NUMBER :");
clasnum=in.nextInt();
System.out.println("ENTER THE SECTION NUMBER OF THE CLASS :");
secnum=in.nextInt();
System.out.println(clasnum+"CLASS "+secnum+" SECTION PERFORMANCE ACCORDING TO RECORDS IS "+sectionavg(clasnum,secnum)+" %");view();
case 4:System.out.println("ENTER THE CLASS NUMBER OF STUDENT:");
clasnum=in.nextInt();
System.out.println("ENTER THE SECTION NUMBER OF THE STUDENT :");
secnum=in.nextInt();
System.out.println("ENTER THE ROLL NUMBER OF THE STUDENT");
roll=in.nextInt();
System.out.println(name[clasnum][secnum][roll]+" SCORED "+per(clasnum,secnum,roll)+" PERCENTAGE");view();
case 5:System.out.println("HAVE A NICE TIME...");break;
default:System.out.println("INVALID INPUT");view();
}
}
void getinfo(int i,int j,int k)
{
System.out.println("Enter Name : ->");
name[i][j][k]=in.next();
System.out.println("Enter marks in sub1 : ->");
sub1[i][j][k]=in.nextFloat();
System.out.println("Enter marks in sub2 : ->");
sub2[i][j][k]=in.nextFloat();
per[i][j][k]=(sub1[i][j][k]+sub2[i][j][k])/2;
}
float per(int i,int j,int k){return per[i][j][k];}
public static void main(String[] args)
{
cpa shivalik=new cpa();
shivalik.start();
}
}
It gets complied and also works for 2 steps after that gives null pointer exception.
So my question is: How to allocate memory to multi-dimensional array object on line 48 using vectors or without them ??
Upvotes: 0
Views: 85
Reputation: 2281
I hve run your code, it seems that sec[]
is not initialized. It crashes when sec[i]=in.nextInt();
Upvotes: 0
Reputation: 533530
Usually when you get a NullPointerException the simplest thing to do is to look at the line getting the exception and ask yourself why is this not initialised? If this doesn't help step through your code in a debugger.
I don't know which line is 48, but this line is clearly wrong.
n[i][j]=in.nextInt();
You use n
as an array of arrays but I can't find the code where you initialise it to anything.
BTW arrays are index from 0, not 1 so you loops should look like
for (int i = 0; i < classes; i++)
Given n
appears to be a temporary variable, you don't appear to need to store it anywhere and I would remove it from your code.
Upvotes: 1