Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8939439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:49:22+00:00 2026-06-15T10:49:22+00:00

My Text file contain data as: sample.txt MEMHEAD 1 1 NA SetString srcCode MEMHEAD

  • 0

My Text file contain data as: sample.txt
MEMHEAD 1 1 NA SetString srcCode

MEMHEAD 1 2 NA SetString memIdnum

LGLNAME 1 5 NA SetString onmfirst

I have created MyClassModel class extending AbstractTableModel as:

    public class MyClassModel extends AbstractTableModel 
{

    Vector data;
    Vector columns;
    public MyTableModel() {
            String line;

            try {
                    FileInputStream fis = new FileInputStream("sample.txt");
                    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                    StringTokenizer st1 = new StringTokenizer(br.readLine(), ",");
                    while (st1.hasMoreTokens())
                           columns.addElement(st1.nextToken());
                    while ((line = br.readLine()) != null) {
                            StringTokenizer st2 = new StringTokenizer(line, ",");
                            while (st2.hasMoreTokens())
                                    data.addElement(st2.nextToken());
                    }
                    br.close();
            } catch (Exception e) {
                    e.printStackTrace();
            }
    }

    public int getRowCount() {
            return data.size() / getColumnCount();
    }

    public int getColumnCount() {
            return columns.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
            return (String) data.elementAt((rowIndex * getColumnCount())
                            + columnIndex);
    }
}
}

In this way I retrieved data from a file. Now when I make a JTable and set model using table.setModel(MyTableModel). I am getting a NullPointerException.

CONTINUED:

The text file data is being displayed in the JTable but JTable also contains some extra rows with some junk data and when I press on those rows
java.lang.ArrayIndexOutOfBoundsException: 180 >= 180 this exception is thrown.
However, I Found out that there is some problem with the line

public Object getValueAt(int rowIndex, int columnIndex) {
        return (String) data.elementAt((rowIndex * getColumnCount()) + columnIndex);
}

But dont know what is the problem.Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:49:23+00:00Added an answer on June 15, 2026 at 10:49 am

    You are adding data to both Vectors with out initializing them.

    Vector data;
    Vector columns;
    

    Initialize them before you add elements.

    Vector data = new Vector();
    Vector columns = new Vector();
    

    Check after doing this whether you are getting NullPointerException or not.

    If still you are getting NPE then I doubt that you didn’t initialize JTable. So post code to make us know where exactly exception is coming.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a txt file, here is some sample data: Id,PriceOne,Company,PriceTwo,PriceThree 11,15.3599997,Japan ltd,12.23,3.1777777 12,,Koyoto
I have a text file which contains data seperated by '|'. I need to
I have Text file that contains data separated with a comma , . How
I have a text file that contains cached data in JSON format. I'm trying
I have a text file containing 5 columns of data. The first column contains
I have a text file that might contain thousands and thousands of numbers(0-9 -->
I have a big text file containing data that needs to be extracted and
I want to get data from a text file to a listview. A sample
I have a file- message.txt that contains raw data, my application reads the file,
I have a data text file which contains this Map2D, [3, 2] Dot3D, [25,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.