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 8914109
In Process

The Archive Base Latest Questions

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

I am having issues with the initVectors during table update. It throws a NullPointerException.

  • 0

I am having issues with the initVectors during table update. It throws a NullPointerException.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at HardwareDbFile.HardwareInventoryUI.update(HardwareInventoryUI.java:47)
    at java.util.Observable.notifyObservers(Observable.java:159)
    at java.util.Observable.notifyObservers(Observable.java:115)
    at HardwareDbFile.HardwareFileWatchdog.actionPerformed(HardwareFileWatchdog.java:36)
    at javax.swing.Timer.fireActionPerformed(Timer.java:312)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

TableModel

package HardwareDbFile;



import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;


public class HardwareFileTableModel extends AbstractTableModel{
    protected Vector data;
    protected Vector columnNames ;
    protected String datafile;

    public HardwareFileTableModel(String file){
        datafile = file;
        initVectors();  
    }

    public void initVectors() {
        String aLine ;
        data = new Vector();
        columnNames = new Vector();
        try {
            FileInputStream fin =  new FileInputStream(datafile);
            try (BufferedReader br = new BufferedReader(new InputStreamReader(fin))) {
                StringTokenizer st1 = new StringTokenizer(br.readLine(), "|");
                while(st1.hasMoreTokens()) {
                    columnNames.addElement(st1.nextToken());
                }
                // extract data
                while ((aLine = br.readLine()) != null) {
                    StringTokenizer st2 = new StringTokenizer(aLine, "|");
                    while(st2.hasMoreTokens()) {
                        data.addElement(st2.nextToken());
                    }
               }
           }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public int getRowCount() {
        return data.size() / getColumnCount();
    }

    @Override
    public int getColumnCount(){
        return columnNames.size();
    }

    @Override
    public String getColumnName(int columnIndex) {
        String colName = "";

        if (columnIndex <= getColumnCount()) {
            colName = (String)columnNames.elementAt(columnIndex);
        }
        return colName;
    }

    @Override
    public Class getColumnClass(int columnIndex){
        return String.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

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

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    }
}

Observer Class

package HardwareDbFile;


import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.Timer;

 public class HardwareFileWatchdog extends Observable implements ActionListener {
     Timer time = new Timer(1000,this); // check every second
     long lastModified;
     String file;

     HardwareFileWatchdog (String string) {
         file = string;
         File f = new File(file);
         lastModified = f.lastModified(); // original timestamp
         time.start();
    }

    @Override
     public void actionPerformed(ActionEvent e) {
         File f = new File(file);
         long actualLastModified = f.lastModified() ;
         if (lastModified != actualLastModified) {
             // the file have changed
             lastModified = actualLastModified;
             setChanged();
             notifyObservers();
         }
     }
}

Main Class snippet

    public class HardwareInventoryUI extends javax.swing.JFrame implements Observer {

    private String datafile = "hardware.dat";
    private String dataFilePath = "C:\\Windows\\Temp\\hardware.dat";
    protected HardwareFileTableModel model;

    /**
    * Creates new form HardwareInventoryUI
    */
    public HardwareInventoryUI() {
        initComponents();

        HardwareFileWatchdog  fileWD;
        Font font;
        font = new Font("SanSerif",Font.PLAIN,14);
        setFont(font);

        // this watchdog (an Observable object) is monitoring any file change
        fileWD = new HardwareFileWatchdog(dataFilePath);
        fileWD.addObserver(this);
    }

    @Override
    public void update(Observable o, Object arg) {
        // reload data because data file have changed
        model.initVectors();
        jTable.repaint();
    }

Button Action to add record

    private void jAddRecordActionPerformed(java.awt.event.ActionEvent evt) {

        String toolID = jToolID.getText();
        String toolName = jToolName.getText();
        String quantity = jQuantity.getText();
        String itemPrice = jItemPrice.getText();
        try {
            FileWriter fstream = new FileWriter(dataFilePath, true);
            BufferedWriter newRecord = new BufferedWriter(fstream);
            newRecord.newLine();
            newRecord.write(toolID + "|"+ toolName + "|" + quantity + "|" + itemPrice );
            newRecord.close();
        } catch (IOException ex) {
            Logger.getLogger(HardwareInventoryUI.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

Process: file is created and loaded in the table model, which works fine. The problem comes when I attempt to add a new record. The record is added to the text file in proper format, but it throws an exception during the attempt to update the table model with the new data. I am at a loss.

  • 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-15T04:38:50+00:00Added an answer on June 15, 2026 at 4:38 am

    model is null. You have not created an instance for it.

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

Sidebar

Related Questions

Having issues mapping a ORACLE TIMESTAMP(9) field to java.sql.Timestamp or java.util.Date in Java Versions:-
Im having issues trying to join a table twice. I have 3 tables: Users,
Having issues with Twitter Bootstrap responsive navigation. The best way to explain this is
Having issues referencing $(this) from within a the nested ajax 'success' function... I know
Am having issues with printing different sized PDF's using GhostScript (V9.05). The PDF in
Still having issues with this problem. Please help if you can. So I am
Im having issues getting this to work, maybe its not even possible? I have
Im having issues vertically positioning text inside of a text input field in Firefox.
I am having issues with networking being very slow.. For testing purposes, I tried
I am having issues passing a dynamic parameter to a JavaScript function using innerHTML.

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.