Results 1 to 4 of 4
  1. #1
    fonso gfx is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default Help on Serializing an array data structure

    Hi everyone,

    I have been working for the past 2 weeks on a program that stores names and pin numbers on a array data structure. Basically, it ask for a pin number and it looks for it in the database. If it's found, it gives you the name associated with that pin number. If it's not found, then the program asks for a name and adds the information to the database.

    Without Serialization the program works well and I don't get any errors.

    When I run my program the first time, it creates file named "PinBook.ser" and I saves my array data structure using the "ObjectOutputStream class" without any errors. However, when I run my program the second time and I try to get a pin or a name from the file, I always get a NullPointerException.

    I been reading my java book all over and I can't find a solution to this problem. It looks like I am not reading my array data structure as I should. Therefore, I am posting my code you can see what I have done.

    Thanks in advance.


    Java Code:
    import java.io.*;
    import javax.swing.JOptionPane;
    
    public class Agenda implements Serializable{
    	
    	private static final long serialVersionUID = 1L;
    	
    	private static final int MAX_SIZE = 30;	
    	private static Book [] list;
    	private static int numOfEntries = 0;
    	
    	public Agenda(){
    		
    		list = new Book[MAX_SIZE];
    		numOfEntries = 0;
    	}
    
    	/**
    	 * Asks the user for a valid pin.
    	 * @return pin number entered
    	 */
    	private static int getPin(){		
    		String input = JOptionPane.showInputDialog(null, "Enter a 4 digits pin number: ",
    					   "PIN NUMBER", JOptionPane.DEFAULT_OPTION);
    		try{
    			Integer pin = Integer.parseInt(input);			
    			return pin;				
    		}catch(NumberFormatException e){					
    				JOptionPane.showMessageDialog(null,
    					"An invalid phone was entered" ,
    					"INPUT ERROR", JOptionPane.ERROR_MESSAGE);
    				
    				System.exit(0);
    		}		
    		return -1;				
    	}
    
    	/**
    	 * asks for the name of the person associated with the pin
    	 * @param pin - a numeric pin number
    	 * @return fullName - name of the person
    	 */
    
    	public static String getName(int pin){		
    		String fullName = JOptionPane.showInputDialog(null, "Enter name for " 
    						 + pin, "NAME", JOptionPane.DEFAULT_OPTION);		
    		return fullName;
    	}
    	
    	/**
    	 * Creates a Book object and adds the pin and name of the person to the
    	 * database.
    	 * @param pinNum - a numeric pin number
    	 * @param fullName - full name of the person
    	 */
    
    	public void add(int pinNum, String fullName){	
    		int i = 0;
    		while(i < numOfEntries && (list[i].pin != pinNum)){
    			i++;
    		}
    		Book entry = new Book(pinNum, fullName);
    		list[i] = entry;			
    		if(i == numOfEntries){
    			numOfEntries++;
    		}				
    	}
    	
    	/**
    	 * 
    	 * @param pinNum - the pin number being searched for
    	 * @return the name of person if found or null if not found.
    	 * 
    	 */
    	public  String lookUp(int pinNum){		
    		int i = 0;
    		while(i < numOfEntries && (list[i].pin != pinNum)){
    			i++;
    		}
    		if(i < numOfEntries){
    			return list[i].name;			
    		}else{		
    			return null;
    		}			
    	}
    
    	public static void main (String [] args){
    		
    		Agenda test = null;		
    		
    		String fullName = null;
    		int pinNum;		
    		boolean done = false;
    		
    		File db = new File("PinBook.ser");
    		
    		if(db.exists()){
    			
    			try{
    				ObjectInputStream ois = new ObjectInputStream(new FileInputStream(db));				
    				list = (Book[]) ois.readObject();				
    				ois.close();
    				
    			}catch(Exception e){
    				
    				String message = "Error reading " + db;
    				JOptionPane.showMessageDialog(null,	message ,
    						"READ ERROR", JOptionPane.ERROR_MESSAGE);
    			}
    		}else{
    			test = new Agenda();
    		}
    		
    		
    		while(!done){
    			
    			pinNum = getPin();
    			
    			if(pinNum == -1){
    				done = true;
    				break;
    			}
    			
    			fullName = test.lookUp(pinNum);
    			
    			if(fullName != null){
    				JOptionPane.showMessageDialog(null,"PIN: " + pinNum + "\nNAME: " + fullName, 
    						"NAME FOUND!", JOptionPane.PLAIN_MESSAGE);
    				
    			}else{
    				fullName = getName(pinNum);				
    				test.add(pinNum, fullName);
    			}				
    		}	
    		
    		try{
    			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(db));
    			oos.writeObject(list);
    			oos.flush();
    			oos.close();
    			
    			
    		}catch(IOException e){			
    			JOptionPane.showMessageDialog(null,	"ERROR CLOSING FILE",
    					"WRITE ERROR", JOptionPane.ERROR_MESSAGE);			
    		}				
    	}
    
    	private class Book implements Serializable{		
    
    		private static final long serialVersionUID = 1L;
    		private int pin;
    		private String name;	
    
    		public Book(int pinNum, String fullName){			
    			pin = pinNum;
    			name = fullName;			
    		}		
    	}	
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The NPE has nothing to do with serialization and all to do with program logic. Look at the line that's throwing the NPE, you'll find an object that the program is trying to dereference and which is null.

  3. #3
    fonso gfx is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks, Fubarable...
    I got it working now.

    I just needed to use the class ObjectInputStream and ObjectOutputStream to read and write all the objects being used as well as the varible numOfEntries.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 11
    Last Post: 04-01-2009, 03:43 AM
  2. data structure and data base??
    By ahmed13 in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2009, 05:48 AM
  3. Replies: 7
    Last Post: 02-11-2009, 09:14 PM
  4. Queue data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:35 PM
  5. data structure code
    By vgvt in forum New To Java
    Replies: 1
    Last Post: 01-17-2008, 02:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •