i try to compile and run
seems like work perfectly but once i click the "add" button(something like save)
error come out
i dont know what problem with the codes

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame1.java
 *
 * Created on Jul 4, 2010, 2:45:14 PM
 */
import java.io.*;
import java.util.*;
import javax.swing.*;

/**
 *
 * @author Rick
 */
public class NewJFrame1 extends javax.swing.JFrame {
    //File data = new File("data.txt");
    DataOutputStream ostream;
    GregorianCalendar aCalendar = new GregorianCalendar();
    @SuppressWarnings("static-access")
    int year = aCalendar.get(aCalendar.YEAR);
    @SuppressWarnings("static-access")
    int month = aCalendar.get(aCalendar.MONTH);
    @SuppressWarnings("static-access")
    int day = aCalendar.get(aCalendar.DAY_OF_MONTH);
    public String name[], date[], subject[], message[], priority[];
    public int add=0;
    public String MAX[]=new String[20]; 

    /** Creates new form NewJFrame1 */
    public NewJFrame1() {
               	try
		{
			ostream = new DataOutputStream(new FileOutputStream("record.dat"));
		}
		catch(IOException e)
		{
			JOptionPane.showMessageDialog(null, e.getMessage());
			System.exit(1);
		}
        initComponents();
        jLabel8.setText(day+"/"+(1+month)+"/"+ year);

    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jComboBox4 = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();

        jLabel6.setText("jLabel6");
        jLabel6.setName("jLabel6"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Memo");

        jLabel1.setText("Name :");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel3.setText("Subject :");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("Message :");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText("Priority :");
        jLabel5.setName("jLabel5"); // NOI18N

        jTextField1.setName("jTextField1"); // NOI18N

        jTextField3.setName("jTextField3"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("Back");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Add Memo");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel7.setText("Date :");
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setName("jLabel8"); // NOI18N

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "10 ", "9", "8", "7", "6", "5", "4", "3", "2", "1" }));
        jComboBox4.setName("jComboBox4"); // NOI18N

        jLabel9.setText("*10 is the highest*");
        jLabel9.setName("jLabel9"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel7)
                            .addComponent(jLabel3))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
                                    .addContainerGap())
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jLabel9))
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addContainerGap(225, Short.MAX_VALUE)))))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
      Object source =evt.getSource();
      if(source == jButton2){
      if(jTextField1.getText().equals("")){
      JOptionPane.showMessageDialog(this,"Please write your name", "Error", JOptionPane.ERROR_MESSAGE);
      }
      else if(jTextField3.getText().equals("")){
      JOptionPane.showMessageDialog(this, "Please enter a subject", "Error", JOptionPane.ERROR_MESSAGE);
      }
      else if(jTextArea1.getText().equals("")){
      JOptionPane.showMessageDialog(this,"Please enter a message", "Error", JOptionPane.ERROR_MESSAGE);
      }
      else{
      JOptionPane.showMessageDialog(this, "Memo sent", "Good Job", JOptionPane.INFORMATION_MESSAGE);
      }

    try
   {
	add++;
	name[add]=jTextField1.getText();
        date[add]=jLabel8.getText();
	subject[add]=jTextField3.getText();
	message[add]=jTextArea1.getText();
	priority[add]=(jComboBox4.getSelectedIndex()+"");
    ostream.writeUTF(name[add]);
    ostream.writeUTF(date[add]);
    ostream.writeUTF(subject[add]);
    ostream.writeUTF(message[add]);
    ostream.writeUTF(priority[add]+"");

    /*ostream.writeInt(jComboBox1.getSelectedIndex());
    ostream.writeUTF(jComboBox2.getSelectedIndex()+"");
    ostream.writeInt(jComboBox3.getSelectedIndex());
    ostream.writeInt(jComboBox4.getSelectedIndex());*/

jTextField1.setText("");
jTextField3.setText("");
jTextArea1.setText("");

}
catch(IOException e){
System.err.println("Error occur");
System.exit(1);

}

}
         // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        NewJFrame page1 = new NewJFrame();
        page1.setVisible(true);
        dispose() ;        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables

}

Recommended Answers

All 4 Replies

At line 251, are you using NewJFrame class or NewJFrame1 class? Also, you have 'NullPointerException' thrown at line 219 when you try to add. How do you compile your code? You need to read and interpret the error message...

line 251 is for the other button.. when user click it, it will go to next frame
i got fews frame..
the line 219 im not sure cause my lecturer give me.. how do i solve it?
i compile and comes out no error. than i run it and test the application only come out the error

I am asking that you are using NewJFrame class which is not the same as your class NewJFrame1. So you are telling me that you are using an external class here?

The NullPointerException (NPE) is a runtime exception. In other words, it will not be caught at compile time because it is a part of memory access.

You are dumping your spaghetti code to the forum and expect people to look at it for you? This may not work... I can tell you only one thing that you need to look for how the variable which cause NPE is initialized. The cause of problem for NPE usually occurs way before (at initialize) it gets to that line.

Just a little help...

public String name[], date[], subject[], message[], priority[];

You have this line above but you DO NOT initialize them anywhere. You cannot use them if you do not tell compiler what SIZE each string array is going to be. This is not JavaScript that will dynamically allocate memory for you if you do not initialize size of variable array. Also, if you initialize a size for it but you are trying to access outside the size, you will get ArrayOutOfBoundException.

PS: If the whole code is from your professor, you should not take any class from him/her again... This is not a good practice in coding -- using no meaning variable names, no information hiding (throw everything inside a method), and spaghetti-liked code.

Just comes to realization... The quoted code line above is not a correct way to declare array variables anyway... It should be somewhat as below...

// Don't be shy to break them all down to each line.
// It is much easier to read and/or make changes later on.
public String[] name;
public String[] date;
public String[] subject;
public String[] message;
public String[] priority;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.