简体   繁体   English

接收空指针异常

[英]Receiving a null pointer exception

I have a class that sets up an array. 我有一个设置数组的类。 Another class extends this class and uses the mentioned array in a method to return another array. 另一个类扩展了该类,并在方法中使用提到的数组以返回另一个数组。 I am receiving a null exception when I do this however when I set up the original array, it works just fine. 我这样做时会收到null异常,但是当我设置原始数组时,它工作得很好。 Not sure what I'm doing wrong here. 不知道我在做什么错。 Still pretty new at this. 还是很新的。

The class that sets up the array (part of it): 设置数组的类(部分数组):

public class InitialGrids { 

int iic,ioc,iman ;
int nic, noc;
double masIC, masOC, masMAN;
double volIC, volOC, volMAN, volP;
double radIC, radOC, radP;

public int iMat[ ];
public double rad[ ];
public double vol[ ];
public double mas[ ];
public double dmas[ ];

public double [ ] radius(int nr)
{
    rad = new double [nr +1];

        for( int k = 0 ; k <= nic ; k++ )
        {
            rad[ k ] = radIC * ((double) k / nic);
            System.out.println("the radius at shell " + k + " is " + rad[k]);

        }

        for( int k = nic + 1 ; k <= noc ; k++ )
        {       
            rad[ k ] = radIC + ( (radOC - radIC) * ((double) (k - nic) / (noc - nic)) );
            System.out.println("the radius at shell " + k + " is " + rad[k]);

        }

        for( int k = noc + 1 ; k <= nr ; k++ )
        {
            rad[ k ] = radOC + ( (radP - radOC) * ((double) (k - noc) / (nr - noc)) );
            System.out.println("the radius at shell " + k + " is " + rad[k]);

        }   

        return(rad);
}

This is the class that extends InitialGrids(part): 这是扩展InitialGrids(part)的类:

public class Compression extends InitialGrids {

double G;
double PI;
double Pave;
double x, z, dy, dz, dydx; // this is for the iteration that will update the density
double delta;
double mix;

public double grav[ ];
public double P[ ];
public double rhon[ ];
public double radn[ ];

public double [ ] gravity( int nr ) 
{
    G = Constants.PI;
    PI = Constants.PI;
    grav = new double [ nr + 1 ];

    for(int k = 1; k <= nr; k++)
    {
        grav[ 0 ] = 0;
        grav[ k ] = ( (G*mas[k]) / (Math.pow(rad[k], 2)) ); 
    }
    return(grav);
}

I am calling these in the main class like: 我在主类中称这些为:

    InitialGrids ig = new InitialGrids();
ig.setup(icMat, ocMat, manMat, nr, masP, fic, foc, fman, fSi, fSi, fO);
 ig.material(nr);
 ig.radius(nr);
 ig.density(nr,fSi, fSi, foc);
 ig.volume(nr);
 ig.dmass(nr);
 ig.mass(nr);

   Compression com = new Compression();
   com.gravity(nr);
   com.pressure(nr);
   com.newDensity(nr);
   com.radn(nr);

"mas[]" is not initialized anywhere in your code. “ mas []”未在代码中的任何地方初始化。 Unless you are initializing it elsewhere, that's your issue. 除非您在其他地方对其进行初始化,否则这就是您的问题。

So, you're breaking nearly every rule here. 因此,您几乎在这里违反了所有规则。 For one thing, the field rad is not initialized until you call a method radius, so newly created instances of InitialGrids are invalid until this method is called. 一方面,在调用方法radius之前不会初始化字段rad ,因此,直到调用此方法后,新创建的InitialGrids实例InitialGrids无效。

So your null pointer is happening because you create an instance of InitialGrids , ig , and call radius , and then a separate instance of Compression and somehow expect the rad in that instance to be related to the one in the ig instance? 因此,空指针正在发生,因为您创建了InitialGridsig和call radius实例,然后创建了Compression单独实例,并以某种方式期望该实例中的radig实例中的rad相关? I don't think so. 我不这么认为。

Why are these classes in an inheritance relationship anyway? 为什么这些类仍然处于继承关系中?

Most likely, instance variable mas and rad should be initialized. 最有可能应初始化实例变量mas和rad。 May be you need to call like this. 可能是您需要这样打电话。

Compression com = new Compression();
com.setup(icMat, ocMat, manMat, nr, masP, fic, foc, fman, fSi, fSi, fO);
com.material(nr);
com.radius(nr);
com.gravity(nr);

The instance named "com" and "ig" is totally different. 名为“ com”和“ ig”的实例完全不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM