Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-23-2012, 07:43 PM   PM User | #1
michem
New to the CF scene

 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
michem is an unknown quantity at this point
Unhappy Java Help

Hi I can't figure out why program just doesn't want to add records to a file. i figure there's something i'm not doing or something like that-- i new to java and have not coded in a while. that being said, help?

my program supposed to accept user information using JTextField and output the information in a .txt file. I use JFileChooser and added a add record method using output. writeObject and output.flush.

here is my code
import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class BillingUI extends JPanel {

/**
*
*/
private static final long serialVersionUID = 1L;

protected final static String names[] = {"Taskname","date", "Timestart", "timestop", "Entertotal", "YourNotes"};

protected JLabel labels[];
protected JTextField fields[];
protected JButton dotask1, dotask2, dotask3;
protected JPanel innerPanelCenter, innerPanelSouth;

protected int size;

public static final int TASKNAME = 0, DATE = 1, TIMESTART =2, TIMESTOP = 3, ENTERTOTAL =4, YOURNOTES = 5 ;

public BillingUI(int mySize) {
size = mySize;
labels = new JLabel[size];
fields = new JTextField[size];

for (int count = 0; count < labels.length; count++)
labels[count] = new JLabel(names[count]);

for(int count = 0; count< fields.length; count++)
fields[count] = new JTextField();

innerPanelCenter = new JPanel();
innerPanelCenter.setLayout(new GridLayout(size, 3));

for (int count = 0; count < size; count++){
innerPanelCenter.add(labels[count]);
innerPanelCenter.add(fields[count]);

}

dotask1 = new JButton();
dotask2 = new JButton();
dotask3 = new JButton();

innerPanelSouth = new JPanel();
innerPanelSouth.add(dotask1);
innerPanelSouth.add(dotask2);
innerPanelSouth.add(dotask3);

setLayout(new BorderLayout());
add(innerPanelCenter, BorderLayout.CENTER);
add(innerPanelSouth, BorderLayout.SOUTH);

validate();


}

public JButton task1Button()
{
return dotask1;
}

public JButton task2Button()
{
return dotask2;
}

public JButton task3Button()
{
return dotask3;
}

public JTextField[] getFields()
{
return fields;
}

public void clearFields()
{
for(int count = 0; count < size; count++)
fields[count].setText(" ");
}

public void setFieldValues(String strings[])
throws IllegalArgumentException
{
if (strings.length != size)
throw new IllegalArgumentException("There must be" + size + " Characters");

for(int count= 0; count < size; count++ )
fields[count].setText(strings[count]);
}

public String[] getFieldValues()
{
String[] values = new String[size];
for(int count =0; count< size; count++)
values[count] = fields[count].getText();


return values;
}
}

import java.io.Serializable;
//import java.util.*;

public class TimeLog implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;
private String TASKNAME;
private String DATE;
private String TIMESTART,TIMESTOP;
private int ENTERTOTAL;
private String YOURNOTES;

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

public TimeLog(String TASKNAME,
String DATE,
String TIMESTART,
String TIMESTOP,
int ENTERTOTAL,
String YOURNOTES)

{
setTASKNAME(TASKNAME);
setDATE(DATE);
setTIMESTART(TIMESTART);
setTIMESTOP(TIMESTOP);
setENTERTOTAL (ENTERTOTAL);
setYOURNOTES(YOURNOTES);
}


// task name
public void setTASKNAME(String tasknm)
{
TASKNAME = tasknm;
}

public String getTASKNAME()
{
return TASKNAME;
}
//date
public void setDATE(String date)
{
DATE = date;
}
public String getdate()
{
return DATE;
}

public void setTIMESTART(String time)
{
this.TIMESTART = time;
}
public String getTIMESTART()
{
return TIMESTART;
}
//time
public void setTIMESTOP(String time)
{
TIMESTOP = time;
}
public String gettime()
{
return TIMESTOP;
}
//enter total
public void setENTERTOTAL(int total)
{
ENTERTOTAL = total;
}

public int getENTERTOTAL()
{
return ENTERTOTAL;
}
//your notes

public void setYOURNOTES(String notes)
{
YOURNOTES = notes;
}

public String getYOURNOTES()
{
return YOURNOTES;
}

}
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

//import javax.swing.table.*;

//import jxl.*;
//import jxl.write.*;





public class CreateFile extends JFrame{

/**
*
*/
private static final long serialVersionUID = 1L;
private ObjectOutputStream output;
BillingUI userInterface;
private JButton enterButton, openButton, exitButton;

public CreateFile() {
super("Enter Productivity Infomation");

userInterface = new BillingUI(6);
getContentPane().add(userInterface, BorderLayout.CENTER);

openButton = userInterface.task1Button();
openButton.setText("save data to");

openButton.addActionListener(
new ActionListener(){

public void actionPerformed(ActionEvent event)
{
openFile();
}
}); // end action listener

exitButton = userInterface.task3Button();
exitButton.setText("Cancel");
exitButton.setEnabled(true);

exitButton.addActionListener(

new ActionListener(){
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});


enterButton = userInterface.task2Button();
enterButton.setText("Enter");
enterButton.setEnabled(true);

enterButton.addActionListener(

new ActionListener(){
public void actionPerformed(ActionEvent event)
{
addRecord();
}
});

addWindowListener(

new WindowAdapter(){

public void windowClosing(WindowEvent event)
{
if(output != null)
addRecord();

closeFile();
}
});

setSize(300,300);
setVisible(true);



}

void openFile()
{

JFileChooser chooseFile = new JFileChooser();


chooseFile.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileNameExtensionFilter filter = new FileNameExtensionFilter("txt", "dat", "xls");
chooseFile.addChoosableFileFilter(filter);

int result = chooseFile.showSaveDialog(this);

if(result == JFileChooser.APPROVE_OPTION)
return;

File filename = chooseFile.getSelectedFile();

if(filename == null || filename.getName().equals(" "))
JOptionPane.showMessageDialog(this, "Invalid File name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);

else{
try{
output = new ObjectOutputStream( new FileOutputStream(filename));

openButton.setEnabled(false);
enterButton.setEnabled(true);
}

catch(IOException ioException){
JOptionPane.showMessageDialog(this, "error opening file", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
void closeFile()
{
try{
output.close();
System.exit(0);
}

catch (IOException ioException){
JOptionPane.showMessageDialog(this, "Error Closing file", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}

public void addRecord()
{




int enternumber =0;


TimeLog record;
String fieldValues[] = userInterface.getFieldValues();

if(!fieldValues[BillingUI.ENTERTOTAL].equals("")){

try{
enternumber = Integer.parseInt(fieldValues[BillingUI.ENTERTOTAL]);


if (enternumber > 0) {

record = new TimeLog(fieldValues[BillingUI.TASKNAME],fieldValues[BillingUI.DATE], fieldValues[BillingUI.TIMESTART],
fieldValues[BillingUI.TIMESTOP], enternumber, fieldValues[BillingUI.YOURNOTES] );

output.writeObject(record);
output.flush();
}
else{
JOptionPane.showInputDialog(this, "total field must be an integer", JOptionPane.ERROR_MESSAGE);

}
userInterface.clearFields();
} // end try else block

catch(NumberFormatException | IOException formatException){
JOptionPane.showMessageDialog(this, "bad number dude", "invalid format/n must be integer greater than 0",
JOptionPane.ERROR_MESSAGE);
closeFile();

}

} // end if

}

public static void main(String args[])
{
new CreateFile();
}
}

this is my errors:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at CreateFile.addRecord(CreateFile.java:171)
at CreateFile$3.actionPerformed(CreateFile.java:78)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
michem is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:36 PM.


Advertisement
Log in to turn off these ads.