Question: jTable data problem

Ads

 
 
 

Share on Google+Share on Google+

A Polite Boy
jTable data problem
0 Answer(s)      4 years and 4 months ago
Posted in : Java Beginners

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.(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)

My txt File:

FName Lname Number
 second secondsecond 22
 thired thithird 33
 fourth fourfourr 44
 fifth fiffif 55

Thanks for help!

Ads
View Answers
Ads









Related Tutorials/Questions & Answers:
jTable data problem
jTable data problem  Hello. I have a code that read file and store...","Number"}; List<String> data=new ArrayList<String>(); String...(){ JFrame frame=new JFrame(); JTable table=new JTable(rftl); JPanel panel
jtable problem
jtable problem  how to make a cell text hypertext
Advertisements
problem with JTable - Swing AWT
problem with JTable  hi guys, i was a student and i am very new to swings.i was having an assignment like i need to create a JTable... an action event such that if i check the checkbox outside the JTable,all the checkboxes
problem Scrolling jTable in scrollpane
problem Scrolling jTable in scrollpane  hi i get into a problem of scrolling jtable in scrollpane.Only horizontal scroll is working, vertical scroll...)); tableModel =new DefaultTableModel(rowdata, colname); table = new JTable
problem Scrolling jTable in scrollpane
problem Scrolling jTable in scrollpane  hi i get into a problem of scrolling jtable in scrollpane.Only horizontal scroll is working, vertical scroll...)); tableModel =new DefaultTableModel(rowdata, colname); table = new JTable
problem scrolling jtable
problem scrolling jtable  hi, i've to query the table thousand...;}}; table = new JTable(tableModel...); MAIN PROBLEM HERE'S M TABLE IS NOT PROPERLY SETTING IN SCROLLPANE...REPLY SOON
CONVERT JTable DATA TO PDF FILE
CONVERT JTable DATA TO PDF FILE  HOW TO CONVERT JTable DATA TO .PDF... the jtable data from the jframe and stored the data into the pdf file in the form...(data, col); table = new JTable(model); JScrollPane pane = new JScrollPane(table
Display both image and data into Swing JTable
Display both image and data into Swing JTable  How To Display both image and data into Swing JTable which is retrieved from MySQL database
display dinamic data in JTable - Swing AWT
display dinamic data in JTable  Hi, I need some help to development... and to read data in each files of this directory and to display it in one JTable... in this directory now i want to display the data of each files
adding data to the database with the use of jtable - Java Beginners
adding data to the database with the use of jtable  how can i add data to the database with the use of jtable. and also can able to view the records in the database in the table.. tnx :G
adding data to the database with the use of jtable - Java Beginners
adding data to the database with the use of jtable  how can i add data to the database with the use of jtable. and also can able to view the records in the database in the table.. tnx :G
jtable
jtable  hey i have build a form and i m also able to add data from database to jtable along with checkbox.the only problem is that if i select multiple checkboxes the data doesnt get inserted into new database and if only one
view data from jTextArea to jtable
view data from jTextArea to jtable  good night Please help senior java all, I want to make a brief program of reading data in the text area and then on the show to the j table. I created a new scrip like below but it does
Java insert file data to JTable
Java insert file data to JTable In this section, you will learn how to insert text file data into JTable. Swing has provide useful and sophisticated set...); } }  Through the above code, you can insert the file data into JTable
Extract File data into JTable
Extract File data into JTable In this section, you will learn how to read the data from the text file and insert it into JTable. For this, we have created... the BufferedReader class, we have read the data of the file. This data is then broken
JTable
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
Java convert jtable data to pdf file
Java convert jtable data to pdf file In this tutorial, you will learn how to convert jtable data to pdf file. Here is an example where we have created... have fetched the data from the jtable and save the data to pdf file. Example
JTable
"}; JTable table=new JTable(data,labels); JScrollPane pane=new JScrollPane..., my problem is that this same table to see it only accepts a single line even... to rewrite my program so you can scroll and data exists in more than one line
bliffoscope data analsys problem
bliffoscope data analsys problem  Bliffoscope Data Analysis Problem... problem you have is detecting the Rejectos spaceships and slime torpedos, because... is. In other words, the data it provides is the equivalent of a black-and-white image
JTable
JTable  Values to be displayed in JTextfield when Clicked on JTable Cells
JTable
JTable   how to select a definite cell which containing a similar text containg to the one which the user entering from a jtable at runtime in java
JTable
JTable  i want to delete record from JTable using a MenuItem DELETE. and values of JTable are fetched from database....please reply soon
JTable
JTable  Hi I have problems in setting values to a cell in Jtable which is in a jFrame which implements TableModelListener which has a abstract method tableChanged(TableModelEvent e) . I'll be loading values from data base when
JTable
JTable  need to add values to a JTable having 4 coloumns ,2 of them are comboboxes
Java swing: Export excel sheet data to JTable
Java swing: Export excel sheet data to JTable In this tutorial, you will learn how to read data from excel file and export it to JTable. In swing applications, sometimes, it is required to display the excel file data into the jtable
jtable
jtable  how to get the values from database into jtable and also add a checkbox into it and then when selected the checkbox it should again insert into database the selected chewckbox.plzz help
jtable
jtable  hi Sir i am working netbeans IDE,I have a jtable when i insert values in jtable then i am unable to print all inserted values,For eg if i insert 1,2,3,4,5,6,7,8 values then , i am getting output
Problem in inserting clob data in jsp
Problem in inserting clob data in jsp  how to insert any rich text editor data (which have more than 32766 characters) in a clob type column of oracle database with jsp
Problem in accessing data from Database
Problem in accessing data from Database  hi..... i'm making a project... is text and all others are of currency data type. If i enter 0 or null value in currency column and then try to retrieve data using my servlet coding
Select Employee and display data from access database in a jtable
Select Employee and display data from access database in a jtable  I... a employee's name from a comboBox and the jtable will be filled with all... server, and implement the needed data objects in a database server. The clients
Problem with appending data to the end of file
Problem with appending data to the end of file  MY JSP CODE..." import="java.io.*,data.*" %> <% String name=request.getParameter("name...: package data; import java.io.*; import java.util.*; public class musicja
Tutorials   
Java Spring Hibernate Struts Training What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile? How to create first Java Program? Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly?

Ads

 
Advertisement null

Ads