如何解决空指针异常?

时间:2014-04-09 08:23:38

标签: java swing nullpointerexception

我是Java的新手,所以请帮助我。如何解决空指针异常?

这是我的代码。

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class Layout1 extends javax.swing.JFrame {

DefaultTableModel dtm;

    public Layout1()
    {
        initComponents();
        //readfile();
        dtm = (DefaultTableModel)myjtable.getModel();`here it showing an exception`
        readFile();
    }



private void initComponents()  
{

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


      p1 = new JPanel(new BorderLayout());



      p2 = new JPanel(new BorderLayout());


      p3 = new JPanel(new GridLayout(4, 2));


      p4 = new JPanel(new FlowLayout());


      p5 = new JPanel(new BorderLayout());


      myjtable = new javax.swing.JTable();



      l1 = new JLabel("Item");

      l2 = new JLabel("Price");

      l3 = new JLabel("Quantity");

      l4 = new JLabel("ReorderLevel");

      tf1 = new JTextField();

      tf2 = new JTextField();


      tf3 = new JTextField();

      tf4 = new JTextField();
      p3.add(l1);

      p3.add(tf1);

      p3.add(l2);
      p3.add(tf2);

      p3.add(l3);

      p3.add(tf3);

      p3.add(l4);

      p3.add(tf4);

      b1 = new JButton("Add");
      b2 = new JButton("Update");
      b3 = new JButton("Delete");
      b4 = new JButton("List");
      p4.add(b1);
      p4.add(b2);
      p4.add(b3);
      p4.add(b4);

    /*  t = new JTable();
      t.setModel(new javax.swing.table.DefaultTableModel(
              new Object[][]{
                  //{null, null, null, null},
                  //{null, null, null, null},
                  //{null, null, null, null}
              },
              new String[]{
                  "Item", "Price", "Quantity", "Reorder"
              }
      ));
      sc = new JScrollPane(t);*/



        myjtable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Item", "Price","quantity","reorder"
            }
        ));
        sc.setViewportView(myjtable);

        l1.setText("Item");

        l2.setText("Price");

        l3.setText("quantity");

       l4.setText("reorder");
       p5.add(sc, BorderLayout.CENTER);
       p2.add(p3, BorderLayout.NORTH);
       p2.add(p4, BorderLayout.SOUTH);
       p1.add(p2, BorderLayout.NORTH);
       p1.add(p5, BorderLayout.CENTER);
       add(p1);

       pack();

    }






private void btnaddActionPerformed(java.awt.event.ActionEvent evt) 

{
       String item = tf1.getText();

       String price = tf2.getText();

       String quantity=tf3.getText();

       String reorder=tf4.getText();


       try {


       String filename= 

"C:\\WINNT\\PROFILES\\palanisamyp\\workspace\\InventoryDBInfile\\Myfile.txt";


               FileWriter myfile = new FileWriter(filename,true);

                PrintWriter outStream = new PrintWriter(myfile);

                outStream.print(item);

                outStream.print("|");

                outStream.print(price);

                outStream.print("|");

                outStream.print(quantity);

                outStream.print("|");


                outStream.print(reorder);

                outStream.print("\r\n");

                outStream.close();



                tf1.setText("");
                    tf2.setText("");

                    tf3.setText("");

                    tf4.setText("");

                    myfile.close(); 





       } catch (Exception e) {
e.printStackTrace();}


}
   //end of button action 





//}

    'main method'

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Layout1().setVisible(true);


            }
        });
    }
    private JPanel p1, p2, p3, p4, p5;
    private JLabel l1, l2, l3, l4;
    private JTextField tf1, tf2, tf3, tf4;
    private JButton b1, b2, b3, b4;
    private JScrollPane sc;
    private JTable t;
    private javax.swing.JTable myjtable;

}

1 个答案:

答案 0 :(得分:0)

取消注释JScrollPane的初始化

sc = new JScrollPane(t);