Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Null Pointer Exception - Driving Me Mad!  RSS feed

 
Antony Cartwright
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello im doing a Computer Science course and at the moment doing Java.
I have this wierd problem where i can create a file, add stuff to it, but i cannot append (or read anymore!). Does anyone know whats wrong?

Here's my work:

------------------------------------------------------------------------

import java.io.*;
import javax.swing.*;

public class form extends javax.swing.JFrame {

ObjectOutputStream Output;
ObjectInputStream Input;
File file = new File(System.getProperty("user.dir") + "/bookdata/book.txt");

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

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
txtIsbn = new javax.swing.JTextField();
txtTitle = new javax.swing.JTextField();
txtAuthor = new javax.swing.JTextField();
txtPrice = new javax.swing.JTextField();
txtSupplierno = new javax.swing.JTextField();
txtLastorderdate = new javax.swing.JTextField();
lblIsbn = new javax.swing.JLabel();
lblTitle = new javax.swing.JLabel();
lblAuthor = new javax.swing.JLabel();
lblPrice = new javax.swing.JLabel();
lblSupplierno = new javax.swing.JLabel();
lblLastorderdate = new javax.swing.JLabel();
lblHeader = new javax.swing.JLabel();
btnNew = new javax.swing.JButton();
btnAdd = new javax.swing.JButton();
btnFirst = new javax.swing.JButton();
btnNext = new javax.swing.JButton();
lblCopy = new javax.swing.JLabel();
lblHandling = new javax.swing.JLabel();
lblNavigation = new javax.swing.JLabel();
btnClear = new javax.swing.JButton();

getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(txtIsbn, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 70, 90, -1));

getContentPane().add(txtTitle, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 100, 90, -1));

getContentPane().add(txtAuthor, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 130, 90, -1));

getContentPane().add(txtPrice, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 160, 90, -1));

getContentPane().add(txtSupplierno, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 190, 90, -1));

getContentPane().add(txtLastorderdate, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 220, 90, -1));

lblIsbn.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
lblIsbn.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
lblIsbn.setText("Isbn:");
getContentPane().add(lblIsbn, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, 100, 20));

lblTitle.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
lblTitle.setText("Title:");
getContentPane().add(lblTitle, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, 100, 20));

lblAuthor.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
lblAuthor.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
lblAuthor.setText("Author:");
getContentPane().add(lblAuthor, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, 100, 20));

lblPrice.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
lblPrice.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
lblPrice.setText("Price:");
getContentPane().add(lblPrice, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, 100, 20));

lblSupplierno.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
lblSupplierno.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
lblSupplierno.setText("Supplier No:");
getContentPane().add(lblSupplierno, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, 100, 20));

lblLastorderdate.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
lblLastorderdate.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
lblLastorderdate.setText("Last Order Date:");
getContentPane().add(lblLastorderdate, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 220, 100, 20));

lblHeader.setFont(new java.awt.Font("MS Sans Serif", 1, 36));
lblHeader.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblHeader.setText("Bookshop");
getContentPane().add(lblHeader, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, 320, 60));

btnNew.setText("New Record");
btnNew.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNewActionPerformed(evt);
}
});

getContentPane().add(btnNew, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 80, 100, -1));

btnAdd.setText("Add Record");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});

getContentPane().add(btnAdd, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 110, 100, -1));

btnFirst.setText("First Record");
btnFirst.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnFirstActionPerformed(evt);
}
});

getContentPane().add(btnFirst, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 190, 100, -1));

btnNext.setText("Next Record");
btnNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNextActionPerformed(evt);
}
});

getContentPane().add(btnNext, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 220, 100, -1));

lblCopy.setFont(new java.awt.Font("MS Sans Serif", 3, 12));
lblCopy.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblCopy.setText("Copyright (c) 2006 Antony Cartwright ");
getContentPane().add(lblCopy, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 260, 320, -1));

lblHandling.setFont(new java.awt.Font("MS Sans Serif", 1, 11));
lblHandling.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblHandling.setText("Record Handling");
getContentPane().add(lblHandling, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 60, 100, -1));

lblNavigation.setFont(new java.awt.Font("MS Sans Serif", 1, 11));
lblNavigation.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblNavigation.setText("Navigation");
getContentPane().add(lblNavigation, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 170, 100, -1));

btnClear.setText("Clear Boxes");
btnClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});

getContentPane().add(btnClear, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 140, 100, -1));

pack();
}
// </editor-fold>

private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
txtIsbn.setText("");
txtTitle.setText("");
txtAuthor.setText("");
txtPrice.setText("");
txtSupplierno.setText("");
txtLastorderdate.setText("");
}

private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {
bookRecord record;
try{
record = (bookRecord)Input.readObject();
txtIsbn.setText(record.getIsbn());
txtTitle.setText(record.getTitle());
txtAuthor.setText(record.getAuthor());
txtPrice.setText(Float.toString(record.getPrice()));
txtSupplierno.setText(Integer.toString(record.getSupplierno()));
txtLastorderdate.setText(Integer.toString(record.getLastorderdate()));
} catch(IOException c){
JOptionPane.showMessageDialog(this, "No More Records To Show!", "Error", JOptionPane.ERROR_MESSAGE);
} catch(ClassNotFoundException d){
JOptionPane.showMessageDialog(this, "Class Not Found Error!", "Error", JOptionPane.ERROR_MESSAGE);
} catch(NullPointerException e){
JOptionPane.showMessageDialog(this, "Null Pointer!", "Error", JOptionPane.ERROR_MESSAGE);
}
}

private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {
bookRecord record;
System.out.println(file);
try{
Input = new ObjectInputStream(new FileInputStream(file));
record = (bookRecord)Input.readObject();
txtIsbn.setText(record.getIsbn());
txtTitle.setText(record.getTitle());
txtAuthor.setText(record.getAuthor());
txtPrice.setText(Float.toString(record.getPrice()));
txtSupplierno.setText(Integer.toString(record.getSupplierno()));
txtLastorderdate.setText(Integer.toString(record.getLastorderdate()));
} catch(IOException c){
JOptionPane.showMessageDialog(this, "No first record exists!", "Error", JOptionPane.ERROR_MESSAGE);
} catch(ClassNotFoundException d){
JOptionPane.showMessageDialog(this, "Class Not Found Error!", "Error", JOptionPane.ERROR_MESSAGE);
}
}

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
bookRecord record;
System.out.println(file);


try{
record = new bookRecord(
txtIsbn.getText(),
txtTitle.getText(),
txtAuthor.getText(),
Float.parseFloat(txtPrice.getText()),
Integer.parseInt(txtSupplierno.getText()),
Integer.parseInt(txtLastorderdate.getText()));
Output.writeObject(record);

System.out.println(record);

} catch(IOException b){
JOptionPane.showMessageDialog(this, "Input/Output Error!", "Error", JOptionPane.ERROR_MESSAGE);
} catch(NumberFormatException d){
JOptionPane.showMessageDialog(this, "Textbox Format Error!", "Error", JOptionPane.ERROR_MESSAGE);
} catch(NullPointerException e){
JOptionPane.showMessageDialog(this, "Null Pointer!", "Error", JOptionPane.ERROR_MESSAGE);
}
}

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

try{
Output = new HeadlessObjectOutputStream(new FileOutputStream(file,true));
} catch(IOException a){
JOptionPane.showMessageDialog(this, "Input/Output Error!", "Error", JOptionPane.ERROR_MESSAGE);
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new form().setVisible(true);
}
});
}


--------------------------------------------------------------------

I have a class too which is here:

--------------------------------------------------------------------

/*
* Bookshop.java
*
* Created on 14 May 2006, 20:35
*/

/**
*
* @author Antony Cartwright
*/
import java.io.*;
import javax.swing.*;

public class bookRecord implements Serializable{

private String Isbn;
private String Title;
private String Author;
private float Price;
private int Supplierno;
private int Lastorderdate;

public bookRecord() {
this("","","",0,0,0);
}

public bookRecord(String i, String t, String a, float p, int s, int l) {
setIsbn(i);
setTitle(t);
setAuthor(a);
setPrice(p);
setSupplierno(s);
setLastorderdate(l);
}

public void setIsbn(String i){
Isbn = i;
}

public String getIsbn(){
return Isbn;
}

public void setTitle(String t){
Title = t;
}

public String getTitle(){
return Title;
}

public void setAuthor(String a){
Author = a;
}

public String getAuthor(){
return Author;
}

public void setPrice(float p){
Price = p;
}

public float getPrice(){
return Price;
}

public void setSupplierno(int s){
Supplierno = s;
}

public int getSupplierno(){
return Supplierno;
}

public void setLastorderdate(int l){
Lastorderdate = l;
}

public int getLastorderdate(){
return Lastorderdate;
}
}

class HeadlessObjectOutputStream extends ObjectOutputStream{
public HeadlessObjectOutputStream(OutputStream Output) throws IOException{
super(Output);
}
protected void writeStreamHeader() throws IOException{
}
}

------------------------------------------------------------------------

CAN ANYONE UNDERSTAND WHATS GOING ON?

Thanks, Antony.
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a lot of code. How about narrowing things down for us? When exactly does this exception occur (how can one reproduce the problem)? Do you get an exception stack trace?
 
Antony Cartwright
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, yes i can cut it down. only in the btn new and btn add.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16006
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the stack trace of the exception, which most likely appears in the console window. It tells you exactly in which line of your source code the exception happens.

Now go to that line in your source code and have a good look. Something is null there that's not supposed to be null at that point. Find out why it is null - maybe you forgot to initialize a variable?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!