Hello. I have a code that read file and store in arraylist and then convert to array(To use for table model)
My class extends abstracttablemodel correctly.
My All Code is:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.swing.table.AbstractTableModel; public class ReadFileToList extends AbstractTableModel{ String[] col={"Fname","Lname","Number"}; List<String> data=new ArrayList<String>(); String[][] Arraydata; public void readtolist() throws IOException{ FileReader fr=new FileReader("D:\\AllUserRecords.txt"); BufferedReader br=new BufferedReader(fr); String line; while((line=br.readLine()) !=null){ data.add(line); System.out.println(line); } br.close(); Arraydata=(String[][]) data.toArray(); } public String getColumnName(int colu){ return col[colu]; } public int getRowCount() { return Arraydata.length; } public int getColumnCount() { return col.length; } public Object getValueAt(int rowIndex, int columnIndex) { return Arraydata[rowIndex][columnIndex]; } }
My main Class is ReadFileToListM:
import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; public class ReadFileToListM { ReadFileToList rftl=new ReadFileToList(); public ReadFileToListM(){ JFrame frame=new JFrame(); JTable table=new JTable(rftl); JPanel panel=new JPanel(); JScrollPane sp=new JScrollPane(table); panel.add(sp); frame.add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(470,470); frame.setVisible(true); } public static void main(String[] args){ new ReadFileToListM(); } }
but it has Exception! Please help me, Thanks.
this is my Exceptions:
Exception in thread "main" java.lang.NullPointerException at Library.ReadFileToList.getRowCount(ReadFileToList.java:30) at javax.swing.JTable.getRowCount(JTable.java:2583) at javax.swing.plaf.basic.BasicTableUI.createTableSize(BasicTableUI.java:1646) at javax.swing.plaf.basic.BasicTableUI.getPreferredSize(BasicTableUI.java:1687) at javax.swing.JComponent.getPreferredSize(JComponent.java:1632) at javax.swing.JViewport.getViewSize(JViewport.java:1018) at javax.swing.ScrollPaneLayout.preferredLayoutSize(ScrollPaneLayout.java:476) at java.awt.Container.preferredSize(Container.java:1616) at java.awt.Container.getPreferredSize(Container.java:1601) at javax.swing.JComponent.getPreferredSize(JComponent.java:1634) at java.awt.FlowLayout.layoutContainer(FlowLayout.java:594) at java.awt.Container.layout(Container.java:1432) at java.awt.Container.doLayout(Container.java:1421) at java.awt.Container.validateTree(Container.java:1519) at java.awt.Container.validateTree(Container.java:1526) at java.awt.Container.validateTree(Container.java:1526) at java.awt.Container.validateTree(Container.java:1526) at java.awt.Container.validateTree(Container.java:1526) at java.awt.Container.validate(Container.java:1491) at java.awt.Window.show(Window.java:820) at java.awt.Component.show(Component.java:1419) at java.awt.Component.setVisible(Component.java:1372) at java.awt.Window.setVisible(Window.java:801) at Library.ReadFileToListM.<init>(ReadFileToListM.java:20) at Library.ReadFileToListM.main(ReadFileToListM.java:24) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Library.ReadFileToList.getRowCount(ReadFileToList.java:30) at javax.swing.JTable.getRowCount(JTable.java:2583) at javax.swing.plaf.basic.BasicTableUI.createTableSize(BasicTableUI.java:1646) at javax.swing.plaf.basic.BasicTableUI.getPreferredSize(BasicTableUI.java:1687) at javax.swing.JComponent.getPreferredSize(JComponent.java:1632) at javax.swing.JViewport.getViewSize(JViewport.java:1018) at javax.swing.ScrollPaneLayout.preferredLayoutSize(ScrollPaneLayout.java:476) at java.awt.Container.preferredSize(Container.java:1616) at java.awt.Container.getPreferredSize(Container.java:1601) at javax.swing.JComponent.getPreferredSize(JComponent.java:1634) at java.awt.FlowLayout.layoutContainer(FlowLayout.java:594) at java.awt.Container.layout(Container.java:1432) at java.awt.Container.doLayout(Container.java:1421) at java.awt.Container.validateTree(Container.java:1519) at java.awt.Container.validateTree(Container.java:1526) at java.awt.Container.validateTree(Container.java:1526) at java.awt.Container.validateTree(Container.java:1526) at java.awt.Container.validateTree(Container.java:1526) at java.awt.Container.validate(Container.java:1491) at java.awt.Window.dispatchEventImpl(Window.java:2427) at java.awt.Component.dispatchEvent(Component.java:4240) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) at java.awt.EventDispatchThread.run(EventDispatchThread.java:121) BUILD STOPPED (total time: 4 seconds)
I have a code that read file and store in arraylist and then convert to array(To use for table model)
My class extends abstracttablemodel correctly.
My All Code is:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.swing.table.AbstractTableModel;
public class ReadFileToList extends AbstractTableModel{ String[] col={"Fname","Lname","Number"}; List<String> data=new ArrayList<String>(); String[][] Arraydata; public void readtolist() throws IOException{ FileReader fr=new FileReader("D:\AllUserRecords.txt"); BufferedReader br=new BufferedReader(fr); String line; while((line=br.readLine()) !=null){ data.add(line); System.out.println(line); } br.close(); Arraydata=(String[][]) data.toArray(); }
public String getColumnName(int colu){ return col[colu]; }
public int getRowCount() { return Arraydata.length; }
public int getColumnCount() { return col.length; }
public Object getValueAt(int rowIndex, int columnIndex) { return Arraydata[rowIndex][columnIndex]; } }
My main Class is ReadFileToListM:
import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable;
public class ReadFileToListM { ReadFileToList rftl=new ReadFileToList(); public ReadFileToListM(){ JFrame frame=new JFrame(); JTable table=new JTable(rftl); JPanel panel=new JPanel(); JScrollPane sp=new JScrollPane(table); panel.add(sp); frame.add(panel); frame.setDefaultCloseOperation(JFrame.EXITONCLOSE); frame.setSize(470,470); frame.setVisible(true); }
public static void main(String[] args){ new ReadFileToListM(); } }
but it has Exception! Please help me, Thanks.
this is my Exceptions:
Exception in thread "main" java.lang.NullPointerException
at Library.ReadFileToList.getRowCount(ReadFileToList.java:30)
at javax.swing.JTable.getRowCount(JTable.java:2583)
at javax.swing.plaf.basic.BasicTableUI.createTableSize(BasicTableUI.java:1646)
at javax.swing.plaf.basic.BasicTableUI.getPreferredSize(BasicTableUI.java:1687)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1632)
at javax.swing.JViewport.getViewSize(JViewport.java:1018)
at javax.swing.ScrollPaneLayout.preferredLayoutSize(ScrollPaneLayout.java:476)
at java.awt.Container.preferredSize(Container.java:1616)
at java.awt.Container.getPreferredSize(Container.java:1601)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1634)
at java.awt.FlowLayout.layoutContainer(FlowLayout.java:594)
at java.awt.Container.layout(Container.java:1432)
at java.awt.Container.doLayout(Container.java:1421)
at java.awt.Container.validateTree(Container.java:1519)
at java.awt.Container.validateTree(Container.java:1526)
at java.awt.Container.validateTree(Container.java:1526)
at java.awt.Container.validateTree(Container.java:1526)
at java.awt.Container.validateTree(Container.java:1526)
at java.awt.Container.validate(Container.java:1491)
at java.awt.Window.show(Window.java:820)
at java.awt.Component.show(Component.java:1419)
at java.awt.Component.setVisible(Component.java:1372)
at java.awt.Window.setVisible(Window.java:801)
at Library.ReadFileToListM.
My txt File:
FName Lname Number second secondsecond 22 thired thithird 33 fourth fourfourr 44 fifth fiffif 55
Thanks for help!