Array of Classes NullPointerException

后端 未结 8 1731
悲&欢浪女
悲&欢浪女 2020-12-12 05:17

This is Element class:

public class Element {

    private String elementName;
    private int atomicNumber;
    private String Symbol;
    priv         


        
8条回答
  •  自闭症患者
    2020-12-12 05:29

    Between those two lines

     Element[] element = new Element[103];
     element[0].setElementName("H");
    

    you need to create element[0], that is to build a new instance of Element. In your code element[0] is null, hence the NullPointerException when you call setElementName.

提交回复
热议问题