Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I';m在执行后获得JTabbed窗格索引的Nullpointer异常_Java_Swing_User Interface_Nullpointerexception_Jtabbedpane - Fatal编程技术网

Java I';m在执行后获得JTabbed窗格索引的Nullpointer异常

Java I';m在执行后获得JTabbed窗格索引的Nullpointer异常,java,swing,user-interface,nullpointerexception,jtabbedpane,Java,Swing,User Interface,Nullpointerexception,Jtabbedpane,当条件为true时,我试图编写代码以自动从选项卡式窗格中选择选项卡时,我遇到了一个错误,但当我试图在编译代码后执行该代码时,我遇到了一个带有空指针异常的错误!! 问题出在loadData()中;setSelectedTab()上的方法 公共类GUI扩展JFrame实现ActionListener{ private static final long serialVersionUID = 1; ArrayList<Ship> shiplist= new ArrayList<Sh

当条件为true时,我试图编写代码以自动从选项卡式窗格中选择选项卡时,我遇到了一个错误,但当我试图在编译代码后执行该代码时,我遇到了一个带有空指针异常的错误!! 问题出在loadData()中;setSelectedTab()上的方法

公共类GUI扩展JFrame实现ActionListener{

private static final long serialVersionUID = 1;

ArrayList<Ship> shiplist= new ArrayList<Ship>();
private int shipcount = 0;
private int index = 0;
private Ship shipob;
private JTextField ShipYear;
private JTextField ShipName;
private JTextField CrewSize;
private JTextField ShipType;
private JTextField NoOfPass;
private JTextField NoOfCabins;
private JTextField PerFull;
private JTextField CabinRate;
private JTextField SizeCat;
private JTextField PerFilled;
private JTextField LiquidType;
private JTextField Capacity;
private ObjectInputStream inStream;
private ObjectOutputStream outStream;
private JTabbedPane tab;
private CruiseShip cruiseShip;
private DryCargoShip dryShip;
private LiquidCargoShip liquidShip;
JLabel message=new JLabel();
}


在构造函数中,您正在初始化本地
JTabbedPane
,而不是在类中声明并在方法
loadData
中访问的。 请以正确的方式初始化变量
选项卡
,即

this.tab=new JTabbedPane();
而不是

JTabbedPane tab=new JTabbedPane();

非常感谢你!!感谢您完成了漫长的课程并帮助我:)
public static void main(String[] args) 
{
    GUI Gui = new GUI();
    Gui.readData();
    Gui.loadData();
    Gui.index = 0;
    Gui.met();
    //Gui.shipcount= shiplist.size();
    Gui.setVisible(true);

}

public void actionPerformed(ActionEvent e) 
{
    Container container = getContentPane();
    String string = e.getActionCommand();
    if (string.equals("Next")) 
    {
        nextShip(); 
    } 
    else if (string.equals("Previous")) 
    {
        previousShip();
    } 
    else if (string.equals("Exit")) 
    {
        writeData();
        System.exit(0);
    } 
    else 
    {
        System.out.println("Error");
    }
}

public void readData()
{
    inStream=null;
    try
    {
    inStream= new ObjectInputStream(new FileInputStream("shipdata.dat"));
     try {
         this.shipcount = 0;
         do {
             shiplist.add((Ship)inStream.readObject());
             shipcount++;
         } while (true);
     }
     catch (EOFException obj1) 
     {
         System.out.println("Entered EOF");
     }
     catch (ClassNotFoundException obj2) 
     {
         System.out.println("Class Error: " + obj2.getMessage());
     }
     this.inStream.close();
 }
 catch (FileNotFoundException obj3) 
    {
     System.out.println("File Error: " + obj3.getMessage());
 }
 catch (IOException obj4) 
    {
     System.out.println("IO Error in read file data: " + obj4.getMessage());
 }
}
public void met()
{

    shipcount= shiplist.size();
}



 public void loadData() {
        shipob = shiplist.get(index);
        ShipName.setText(shipob.getShipName());
        ShipYear.setText(shipob.getYear());
        String a=""+shipob.getCrewSize();
        CrewSize.setText(a);
        ShipType.setText(shipob.getShipType());
        if (shipob.getShipType().equals("Cruise Ship")) 
        {   
            System.out.println("In Cruise ship");
            cruiseShip = (CruiseShip)shipob;
            String b=""+cruiseShip.getCabinRate();
            CabinRate.setText(b);
            NoOfPass.setText(Integer.toString(cruiseShip.getNoOfPass()));
            NoOfCabins.setText(Integer.toString(cruiseShip.getNoOfCabins()));
            PerFull.setText(Double.toString(cruiseShip.getPerFull() * 100.0));
            tab.setSelectedIndex(0);  // ERROR IN SELECT INDEX - NULL POINTER EXCEPTION

        } 



else if (shipob.getShipType().equals("Dry Cargo Ship")) 
    {
        System.out.println("In Dry ship");
        dryShip = (DryCargoShip)shipob;
        SizeCat.setText(dryShip.getSize());
        String c=" "+dryShip.getPerFilled()*100.0;
        PerFilled.setText(c);
        try{
        tab.setSelectedIndex(1);}
        catch(Exception e){
            System.out.println(e);
        }
    } 
    else if (shipob.getShipType().equals("Liquid Cargo Ship")) 
    {   
        System.out.println("In LC ship");
        liquidShip = (LiquidCargoShip)shipob;
        LiquidType.setText(liquidShip.getLiqType());
        Capacity.setText(Integer.toString(liquidShip.getCapacity()));
        tab.setSelectedIndex(2);
    }
}




public void nextShip() {
    if (index == shipcount - 1) {
        message.setText("Already at last record   ");
    } 
    else {
        message.setText("");
        index++;
        loadData();
    }
}

    public void previousShip() {
        if (index == 0) 
        {
            message.setText("Already at first record     ");
        } 
        else 
        {
            message.setText("");
            index--;
            loadData();
        }
    }

    public void writeData() 
    {
        try {
            outStream = new ObjectOutputStream(new FileOutputStream("shipdata.dat", false));
            for (Ship ship : shiplist) 
            {
                outStream.writeObject(ship);
            }
            outStream.flush();
             outStream.close();
        }
        catch (IOException obj1) {
            System.out.println("IO Error in writing the data: " + obj1.getMessage());
        }
    }
    }   
this.tab=new JTabbedPane();
JTabbedPane tab=new JTabbedPane();