我在执行后获得了JTabbed窗格索引的Nullpointer异常

时间:2016-11-28 20:03:04

标签: java swing user-interface nullpointerexception jtabbedpane

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

上的方法
import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    import java.awt.event.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowListener;

public class GUI extends 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();

公共GUI() {

setSize(2000,300);
setTitle("Ship Data");
Container content= getContentPane();
content.setBackground(Color.GRAY);
content.setLayout(new BorderLayout());
JMenuBar menu=new JMenuBar();
JMenuItem menue= new JMenuItem("Exit");
menue.addActionListener(this);
menu.add(menue);
setJMenuBar(menu);
this.addWindowListener(new WindowDestroyer());

JPanel panel1= new JPanel();
panel1.setBackground(Color.YELLOW);
panel1.setLayout(new GridLayout(2,2));
panel1.add(new JLabel("Ship Name:"));
ShipName= new JTextField(50);
panel1.add(ShipName);
panel1.add(new JLabel("Ship Year:"));
ShipYear= new JTextField(50);
panel1.add(ShipYear);
panel1.add(new JLabel("Crew Size:"));
CrewSize= new JTextField(50);
panel1.add(CrewSize);
panel1.add(new JLabel("Ship Type:"));
ShipType= new JTextField(50);
panel1.add(ShipType);
content.add(panel1, BorderLayout.NORTH);
JPanel panel2= new JPanel();
panel2.setLayout(new BorderLayout());
JPanel panel3= new JPanel();
panel3.setBackground(Color.CYAN);
panel3.setLayout(new FlowLayout());
JButton Button1= new JButton("Next");
Button1.addActionListener(this);
panel3.add(Button1);
JButton Button2= new JButton("Previous");
Button2.addActionListener(this);
panel3.add(Button2);
panel2.add(panel3,BorderLayout.WEST);
panel2.add(message,BorderLayout.EAST);
JTabbedPane tab=new JTabbedPane();
content.add(panel2, BorderLayout.SOUTH);
JPanel cs= new JPanel();
//JPanel z= new JPanel();
cs.setBackground(Color.ORANGE);
cs.setLayout(new GridLayout(2,2));
cs.add(new JLabel("No of Passenger: "));
NoOfPass= new JTextField(25);
cs.add(NoOfPass);
cs.add(new JLabel("No of Cabins: "));
NoOfCabins= new JTextField(25);
cs.add(NoOfCabins);
cs.add(new JLabel("Percentage Full: "));
PerFull= new JTextField(25);
cs.add(PerFull);
cs.add(new JLabel("Cabin Rate: "));
CabinRate= new JTextField(25);
cs.add(CabinRate);
tab.addTab("Cruise Ship", cs);
JPanel ds= new JPanel();
//JPanel x= new JPanel();
ds.setBackground(Color.ORANGE);
ds.setLayout(new GridLayout(1,2));
ds.add(new JLabel("Size Category : "));
SizeCat= new JTextField(25);
ds.add(SizeCat);
ds.add(new JLabel("Percentage Filled : "));
PerFilled= new JTextField(25);
ds.add(PerFilled);
tab.addTab("Dry Cargo Ship", ds);
JPanel ls= new JPanel();
//JPanel y= new JPanel();
ls.setBackground(Color.ORANGE);
ls.setLayout(new GridLayout(1,2));
ls.add(new JLabel("Liquid Type : "));
LiquidType= new JTextField(25);
ls.add(LiquidType);
ls.add(new JLabel("Capacity : "));
Capacity= new JTextField(25);
ls.add(Capacity);
tab.addTab("Liquid Cargo Ship", ls);
content.add(tab,BorderLayout.CENTER);

}

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

1 个答案:

答案 0 :(得分:1)

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

this.tab=new JTabbedPane();

而不是

JTabbedPane tab=new JTabbedPane();