多维数组对象的内存位置

时间:2013-12-19 10:56:19

标签: java arrays memory multidimensional-array core

它得到了编译,并且在得到空指针异常后也可以工作2步。     Q; - >如何使用向量或没有它们在第48行上为多维数组对象分配内存??

 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();
}   
   }

它得到了编译,并且在得到空指针异常之后也可以工作2步。

所以我的问题是:如何使用向量或不使用向量将行内容分配给第48行的多维数组对象?

2 个答案:

答案 0 :(得分:1)

通常当你得到一个NullPointerException时,最简单的事情就是查看获取异常的行,并问自己为什么没有初始化?如果这无助于在调试器中逐步执行代码。

我不知道哪一行是48,但这一行显然是错误的。

n[i][j]=in.nextInt();

您使用n作为数组数组,但我找不到将其初始化为任何内容的代码。

BTW数组是0的索引,而不是1,所以你的循环应该看起来像

for (int i = 0; i < classes; i++)

鉴于n似乎是一个临时变量,您似乎不需要将其存储在任何位置,我会将其从您的代码中删除。

答案 1 :(得分:0)

我已经运行了您的代码,似乎sec[]未初始化。它在sec[i]=in.nextInt();

时崩溃