创建对象期间发生Java空指针异常

创建对象期间发生Java空指针异常,java,android,nullpointerexception,Java,Android,Nullpointerexception,一切似乎都很好,但我无法在另一个类中创建新对象。我只得到了“java.lang.Null.Pointer.Exception” 实际上,我有一个F16类,用于创建F16飞机。现在,当我尝试在MainActivity中创建一个新对象来显示飞机时,会显示此错误 main活动类 public class MainActivity extends Activity { LinearLayout Map=null; @Override protected void onCr

一切似乎都很好,但我无法在另一个类中创建新对象。我只得到了
“java.lang.Null.Pointer.Exception”
实际上,我有一个F16类,用于创建F16飞机。现在,当我尝试在MainActivity中创建一个新对象来显示飞机时,会显示此错误

main活动类

public class MainActivity extends Activity {    
    LinearLayout Map=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Map=(LinearLayout)findViewById(R.id.Map);
        try{
        F16 myF16=new F16();        
        myF16.CreateAirCraft(myF16.CoordinateX,myF16.CoordinateY, this, Map);
        }
        catch(Exception ex){
        Log.d("Er", ex.toString());     
        }
    }
public class F16 extends AirCraft {
Context context;
private int Image=context.getResources().getIdentifier("F16" , "drawable", context.getPackageName());
LinearLayout PlaneBody=null;//a container used to hold All information of Aircraft
TextView PlaneName=null;// mentions to AirCraft name
ImageView PlaneImg=null;//mentions to AirCraft Image
public void CreateAirCraft(byte X , byte Y,Context Ctx,LinearLayout Map){
        Name="F-16";    
        PlaneBody=new LinearLayout(Ctx);
        PlaneName=new TextView(Ctx);
        PlaneImg=new ImageView(Ctx);
        PlaneName.setText(Name);
        PlaneImg.setImageResource(Image);
        PlaneBody.addView(PlaneName);
        PlaneBody.addView(PlaneImg);
        Map.addView(PlaneBody);
    }
}
public abstract class AirCraft {
   public String Name="";
   public byte CoordinateX=0;
   public byte CoordinateY=0;
   public byte Weight=0;
}
F16级

public class MainActivity extends Activity {    
    LinearLayout Map=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Map=(LinearLayout)findViewById(R.id.Map);
        try{
        F16 myF16=new F16();        
        myF16.CreateAirCraft(myF16.CoordinateX,myF16.CoordinateY, this, Map);
        }
        catch(Exception ex){
        Log.d("Er", ex.toString());     
        }
    }
public class F16 extends AirCraft {
Context context;
private int Image=context.getResources().getIdentifier("F16" , "drawable", context.getPackageName());
LinearLayout PlaneBody=null;//a container used to hold All information of Aircraft
TextView PlaneName=null;// mentions to AirCraft name
ImageView PlaneImg=null;//mentions to AirCraft Image
public void CreateAirCraft(byte X , byte Y,Context Ctx,LinearLayout Map){
        Name="F-16";    
        PlaneBody=new LinearLayout(Ctx);
        PlaneName=new TextView(Ctx);
        PlaneImg=new ImageView(Ctx);
        PlaneName.setText(Name);
        PlaneImg.setImageResource(Image);
        PlaneBody.addView(PlaneName);
        PlaneBody.addView(PlaneImg);
        Map.addView(PlaneBody);
    }
}
public abstract class AirCraft {
   public String Name="";
   public byte CoordinateX=0;
   public byte CoordinateY=0;
   public byte Weight=0;
}
飞机等级

public class MainActivity extends Activity {    
    LinearLayout Map=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Map=(LinearLayout)findViewById(R.id.Map);
        try{
        F16 myF16=new F16();        
        myF16.CreateAirCraft(myF16.CoordinateX,myF16.CoordinateY, this, Map);
        }
        catch(Exception ex){
        Log.d("Er", ex.toString());     
        }
    }
public class F16 extends AirCraft {
Context context;
private int Image=context.getResources().getIdentifier("F16" , "drawable", context.getPackageName());
LinearLayout PlaneBody=null;//a container used to hold All information of Aircraft
TextView PlaneName=null;// mentions to AirCraft name
ImageView PlaneImg=null;//mentions to AirCraft Image
public void CreateAirCraft(byte X , byte Y,Context Ctx,LinearLayout Map){
        Name="F-16";    
        PlaneBody=new LinearLayout(Ctx);
        PlaneName=new TextView(Ctx);
        PlaneImg=new ImageView(Ctx);
        PlaneName.setText(Name);
        PlaneImg.setImageResource(Image);
        PlaneBody.addView(PlaneName);
        PlaneBody.addView(PlaneImg);
        Map.addView(PlaneBody);
    }
}
public abstract class AirCraft {
   public String Name="";
   public byte CoordinateX=0;
   public byte CoordinateY=0;
   public byte Weight=0;
}

在F16类中使用上下文作为参数生成构造函数 比如说

public F16(Context context)
{
Image=context.getResources().getIdentifier("F16" , "drawable", context.getPackageName());
}
并从主活动传递上下文

F16 myF16=new F16(this); 

NullPointerException
是由
F16
类的第3行引起的

Context context;
private int Image=context.getResources().getIdentifier("F16" , "drawable", context.getPackageName());
字段
context
隐式初始化为
null
。然后初始化
Image
并调用
context.getResources()
,但由于
context
为空,因此您将获得NPE。

请尝试下面的代码

public class F16 extends AirCraft {

Context context;
private int Image;
LinearLayout PlaneBody=null;//a container used to hold All information of Aircraft
TextView PlaneName=null;// mentions to AirCraft name
ImageView PlaneImg=null;//mentions to AirCraft Image

public F16(Context context)
{
    this.context = context;
    Image=context.getResources().getIdentifier("F16" , "drawable", context.getPackageName());
}

public void CreateAirCraft(byte X , byte Y,Context Ctx,LinearLayout Map){
        Name="F-16";    
        PlaneBody=new LinearLayout(Ctx);
        PlaneName=new TextView(Ctx);
        PlaneImg=new ImageView(Ctx);
        PlaneName.setText(Name);
        PlaneImg.setImageResource(Image);
        PlaneBody.addView(PlaneName);
        PlaneBody.addView(PlaneImg);
        Map.addView(PlaneBody);
    }
}

@我只得到了“java.lang.Null.Pointer.Exception”,没有更多!在发布日志之前,使用
Log.d(“Er”,例如toString(),e)启用stacktrace。您怎么可能只得到
java.lang.Null.Pointer.Exception
什么都没有。那是不可能的。你的电话线是什么error@curiousdogPost你这个笨蛋。我认为您的logcat抛出了java.lang.NullPointerExceptionBTW:这就是为什么会出现错误的答案。“Android Weblineindia”已经回答了如何修复它。他在F16 myF16=new F16()中得到了错误@Andreas@PiyushMittal他是,但是
new F16()
调用执行字段初始值设定项,并且是初始值设定项导致NPE。它似乎有效,因为它显示飞机的名称,但不显示图像。你认为呢?你缺少了
this.context=context。现在它在构造函数中初始化了,
createAircrafter
方法可能不再需要将
Context
作为参数,当然,除非它可能是不同的上下文。图像存储在哪里?