GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Jul-2009, 10:30
din89pm din89pm is offline
New Member
 
Join Date: Jul 2009
Posts: 2
din89pm is on a distinguished road

Please Check the difference between this 2 java programs


Hie every1, I am facing the same problem as my collegue Exception in thread "main" java.lang.NullPointerException.
We both urgently need a solution for this... can someone fix this error and paste the code for the fixed error? I am still weak in my logic, Your help is sincerely appreciated.

Below is the coding that has no compilation error, process completes but when 2nd entree is entered there will be an error.

Item Class
JAVA Code:
 // ITEM CLASS

public class Item{
	String itemNumber;
	String itemDesc;
	double sellPrice;
	int quantity;
	public boolean duplicate = true; //Declaration for equals method
	private static int noEntries = 0;
	
	public Item(String itemNumber, String itemDesc, double sellPrice, int quantity) {
    	setItemNumber(itemNumber);
    	setItemDesc(itemDesc);
    	setSellPrice(sellPrice);
    	setQuantity(quantity);
    	
    
    } //Constructor with arguments
    
    public void setItemNumber(String itemNumber){
    	this.itemNumber = itemNumber;
    } //Set method for itemNumber
    
    public String getItemNumber(){
    	return itemNumber;
    } //Get method for itemNumber
    
    public void setItemDesc(String itemDesc){
    	this.itemDesc = itemDesc;
    } //Set method for itemDesc
    
    public String getItemDesc(){
    	return itemDesc;
    } //Get method for ItemDesc
    
    public void setSellPrice(double sellPrice){
    	this.sellPrice = sellPrice;
    } //Set method for sellPrice
    
    public double getSellPrice(){
    	return sellPrice;
    } //Get method for sellPrice 
    
    public void setQuantity(int quantity){
    	this.quantity = quantity;
    } //Set method for quantity
    
    public int getQuantity(){
    	return quantity;
    }
    
    public void makeCopy (String itemNumber, String itemDesc, double sellPrice, int quantity) {
    	
    	setItemNumber(itemNumber);
    	setItemDesc(itemDesc);
    	setSellPrice(sellPrice);
    	setQuantity(quantity);
    }
    
    public int getEntries () {
    	return noEntries;
    }
    
     public boolean searchName(String itemNumber) {
    	if(this.itemNumber.equals(itemNumber) == true)
    		return true;
    	
    	else
    		return false;
    }
    
    	public boolean equals (String itemNumber, String itemDesc, double sellPrice, int quantity) {
    	if(this.itemNumber.equals(itemNumber) == true && this.itemDesc.equals(itemDesc) == true &&
       		this.sellPrice == sellPrice && this.quantity == quantity)
       		return true;
       		
       	else
       		return false;		
    } //Equals method that returns true if two objects are the same
} 


Print Class
JAVA Code:
 //PRINT CLASS

public class Print {

	public static void option () {
    	System.out.println("1. Add Item Number");
    	System.out.println("2. Generate List of Record Entered");
    	System.out.println("3. Search Record by Item Number");
    	System.out.println("4. Modifiy Record");
    	System.out.println();    	
    	System.out.println("5. Exit");
    	System.out.println();
    	System.out.print("Enter your choice : ");
    }
    
    public static void sample () {
    	System.out.println("Sample of record to be keyed in correctly to avoide errors.");
    	System.out.println("***********************************************************");
    	System.out.println("Enter Name         : xxxxxxxxx");
    	System.out.println("Enter Address      : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    	System.out.println("Enter Phone Number : 0123456789");
   		System.out.println("Enter Email        : [email]username@provider.com[/email]");
    	System.out.println("###########################################################\n");
    }
    
    public static void duplicateError () {
    	System.out.println("\nThis record has been entered.");
      	System.out.println("Duplicate record are not allow.\n");
    }
    
    public static void createBy () {
    	System.out.println("\n\nThank you for using.");
    	System.out.println("xxxx & xxxx, Q3.");
    }
}

Main Program
JAVA Code:
//MAIN PROGRAM
import java.util.Scanner;
 
public class ItemProgram {
    
    public static void main(String[] args) {
    	
    	Scanner input = new Scanner(System.in);
    	
    	Item[] items = new Item[100];
    	
    	int i = 0;//Used in for-loop counts
    	int j = 0;//Used in for-loop counts
    	int entries = 0;//Record entries of records
    	int first = 0;//Check is the first record to be entered
    	boolean duplicate = true;//Check for duplication of record
    	boolean result = true;//Track seaching
    	
    	int optionMenu = 0;//Used for menu
    	String optionContinue;//Used for continue option
    	
    	//do-while loop for user to prompt back to main menu(optionMenu)
    	do{
    		Print.option();//print menu
    		optionMenu = input.nextInt();
    		
    		//Add New Record
    		if(optionMenu == 1) {
    			System.out.println("\nAdd New Item");
    			System.out.print("No. of Entries : ");//Record number of entries at a time
    			int looping = input.nextInt();
    			
    			System.out.println();
    			Print.sample();//Sample to guide users
    			
    			//Number of entries's for-looping
    			for(i = 0; i < looping; ++i) {
       				System.out.print("Enter item number         : ");
       				input.useDelimiter("\n");
       				String itemNumber = input.next();
       				
       				System.out.print("Enter item description      : ");
       				input.useDelimiter("\n");
       				String itemDesc = input.next();
       					
       				System.out.print("Enter selling price : ");
       				double sellPrice = input.nextDouble();

       				System.out.print("Enter quantity        : ");
       				int quantity = input.nextInt();
       				
       				System.out.println("===========================================================");
					
					
					
					//If first time key in date run statement
       				if(first == 0){
       					++first;//Track if is the first record key in
       					items[i] = new Item(itemNumber, itemDesc, sellPrice, quantity);
       				}
       				//Else continue key in record		
       				else{
       					entries = items[0].getEntries();//Get number of entries entered
       					for(j=0; j<entries; ++j){
       						
       						duplicate = items[j].equals(itemNumber, itemDesc, sellPrice, quantity);//Check for duplication of entries
       						
       						if(duplicate == true) {
       							Print.duplicateError();
       							--i;
       							--looping;
       							break;
       						}
       					}
       					if(duplicate == false)//else save record
       						items[entries] = new Item(itemNumber, itemDesc, sellPrice, quantity);
       				}	
    			}
    		}
    		
    		//Generate List of Record Entered
    		else if(optionMenu == 2) {
    			System.out.println("Generate List of Record Entered");
    			entries = items[0].getEntries();//Get number of record entered
    			
    			for(i=0; i<entries; ++i) {
    				System.out.println(i+1 + ". " + items[i].getItemNumber());
    			}

    			System.out.print("\nItem Number no : ");
    			int searchPerson = input.nextInt();
    			searchPerson = searchPerson - 1;
    			//Print record searched
    			System.out.println("Item Number     : " + items[searchPerson].getItemNumber());
    			System.out.println("Item Description  : " + items[searchPerson].getItemDesc());
    			System.out.println("Selling Price : RM " + items[searchPerson].getSellPrice());
    			System.out.println("Quantity    : " + items[searchPerson].getQuantity());
    		}
    		
    		//Search Record by Name
    		else if(optionMenu == 3) {
    			System.out.println("Search Record by Number");
    			System.out.print("Item Number : ");
    			input.useDelimiter("\n");
    			String searchName = input.next();
    			
    			entries = items[0].getEntries();//Get entries of record entered
    			
    			for(i=0; i<entries; ++i) {
    				
    				boolean equals = items[i].searchName(searchName);//Search record
    				
    				if(equals == true) {//Print if record found
    					System.out.println("\nRecord no. " + (i+1));
    					System.out.println("Item Number     : " + items[i].getItemNumber());
    					System.out.println("Item Description  : " + items[i].getItemDesc());
    					System.out.println("Selling Price: " + items[i].getSellPrice());
    					System.out.println("Quantity    : RM" + items[i].getQuantity());
    					
    					result = true;//Track if the record is found or not
    				}
    				else
    					result = false;//Track if the record is found or not
    			}
    			
    			if(result == false)//Print if record not found
    			System.out.println("Record not found.");
    		}
    		
    		//Modifiy Record
    		else if(optionMenu == 4) {
    			System.out.print("Entry No. : ");
    			int searchEntry = input.nextInt();
    			
    			System.out.print("\n\nEnter Item Number      : ");
       			input.useDelimiter("\n");
       			String itemNumber = input.next();
       				
       			System.out.print("Enter Item Description      : ");
       			input.useDelimiter("\n");
       			String itemDesc = input.next();
       					
       			System.out.print("Enter Selling Price : ");
       			double sellPrice = input.nextDouble();
       			
       			System.out.print("Enter Quantity       : ");
       			int quantity = input.nextInt();
       			
       			entries = items[0].getEntries();//Retrieve record entered
       			//Check for duplication
       			for(i = 0; i<entries; ++i) {
       				duplicate = items[i].equals(itemNumber, itemDesc, sellPrice, quantity);
       			}
       			
       			if(duplicate == true)//print if duplicated record are found
       				Print.duplicateError();
       					
       			else//Save if it not a duplicated record
       				items[searchEntry-1].makeCopy(itemNumber, itemDesc, sellPrice, quantity);
    		}  //LAST CHECKED UNTIL HERE 9:04PM
    		//Exit program 
    		else if(optionMenu == 5)
    			break;
    		//Check for continue looping
    		System.out.print("Do you want to continue (Y/N)?");
			optionContinue = input.next();
			
			System.out.println();
    	}while(optionContinue.equals("N") == false && optionContinue.equals("n") == false);
    	
    	Print.createBy();
    }
    
}
  #2  
Old 11-Jul-2009, 10:37
din89pm din89pm is offline
New Member
 
Join Date: Jul 2009
Posts: 2
din89pm is on a distinguished road

Re: 2nd coding that works


Below is the second version of the coding the difference from the first is that the variables are changed and the data types are different.
1.) No compilation errors
2.) All methods and functions are working with no problem
3.) No "Exception in thread "main" java.lang.NullPointerException" error

Person class
JAVA Code:
public abstract class Person {
	private String title;
	private String name;
	private String address;
	private long telNo;
	private String email;
	private static int noEntries = 0;
	private boolean duplicate = true;

    protected Person(String title, String name, String address, long telNo, String email) {
    	setTitle(title);
    	setName(name);
    	setAddress(address);
    	setTelNo(telNo);
    	setEmail(email);
    	noEntries++;
    } 
    
    protected void setTitle (String title) {
    	this.title = title;
    }
    protected String getTitle () {
    	return title;
    }
    
    protected void setName (String name) {
    	this.name = name;
    }
    protected String getName () {
    	return name;
    }
    
    protected void setAddress (String address) {
    	this.address = address;
    }
    protected String getAddress () {
    	return address;
    }
    
    protected void setTelNo (long telNo) {
    	this.telNo = telNo;
    }
    protected long getTelNo () {
    	return telNo;
    }
    
    protected void setEmail (String email) {
    	this.email = email;
    }
    protected String getEmail () {
    	return email;
    }
    
    public String toString () {
    	return "Name: " + title + " " + name + "\nAddress: " + address + "\nTel No.: " + telNo + "\nEmail: " + email;
    }
    
    protected int getEntries () {
    	return noEntries;
    }
    
    public boolean equals (String name, String address, long telNo, String email) {
    	if(this.name.equals(name) == true && this.address.equals(address) == true &&
       		this.telNo == telNo && this.email.equals(email) == true)
       		return true;
       		
       	else
       		return false;		
    }
    
    public boolean searchName(String name) {
    	if(this.name.equals(name) == true)
    		return true;
    	
    	else
    		return false;
    }
    public void makeCopy (String name, String address, long telNo, String email) {
    	
    	setName(name);
    	setAddress(address);
    	setTelNo(telNo);
    	setEmail(email);
    }
} 

Print class
JAVA Code:
public class Print {

	public static void option () {
    	System.out.println("1. Add New Record");
    	System.out.println("2. Generate List of Record Entered");
    	System.out.println("3. Search Record by Name");
    	System.out.println("4. Modifiy Record");
    	System.out.println();    	
    	System.out.println("5. Exit");
    	System.out.println();
    	System.out.print("Enter your choice : ");
    }
    
    public static void sample () {
    	System.out.println("Sample of record to be keyed in correctly to avoide errors.");
    	System.out.println("***********************************************************");
    	System.out.println("Enter Name         : Kevin Ng Kai Man");
    	System.out.println("Enter Address      : 75-07-07, Sri Sentosa, 58000 Kuala Lumpur");
    	System.out.println("Enter Phone Number : 0123456789");
   		System.out.println("Enter Email        : [email]username@provider.com[/email]");
    	System.out.println("###########################################################\n");
    }
    
    public static void duplicateError () {
    	System.out.println("\nThis record has been entered.");
      	System.out.println("Duplicate record are not allow.\n");
    }
    
    public static void createBy () {
    	System.out.println("\n\nThank you for using.");
    	System.out.println("Created by Kevin Ng Kai Man, Q3.");
    }
}

Main program
JAVA Code:
import java.util.Scanner;
 
public class Assignment1 {
    
    public static void main(String[] args) {
    	
    	Scanner input = new Scanner(System.in);
    	
    	Person[] person = new Person[100];
    	
    	int i = 0;//Used in for-loop counts
    	int j = 0;//Used in for-loop counts
    	int entries = 0;//Record entries of records
    	int first = 0;//Check is the first record to be entered
    	boolean duplicate = true;//Check for duplication of record
    	boolean result = true;//Track seaching
    	
    	int optionMenu = 0;//Used for menu
    	String optionContinue;//Used for continue option
    	
    	//do-while loop for user to prompt back to main menu(optionMenu)
    	do{
    		Print.option();//print menu
    		optionMenu = input.nextInt();
    		
    		//Add New Record
    		if(optionMenu == 1) {
    			System.out.println("\nAdd New Record");
    			System.out.print("No. of Entries : ");//Record number of entries at a time
    			int looping = input.nextInt();
    			
    			System.out.println();
    			Print.sample();//Sample to guide users
    			
    			//Number of entries's for-looping
    			for(i = 0; i < looping; ++i) {
       				System.out.print("Enter Name         : ");
       				input.useDelimiter("\n");
       				String name = input.next();
       				
       				System.out.print("Enter Address      : ");
       				input.useDelimiter("\n");
       				String address = input.next();
       					
       				System.out.print("Enter Phone Number : ");
       				long telNo = input.nextLong();

       				System.out.print("Enter Email        : ");
       				String email = input.next();
       				
       				System.out.println("===========================================================");
					
					
					
					//If first time key in date run statement
       				if(first == 0){
       					++first;//Track if is the first record key in
       					person[i] = new Person(name, address, telNo, email);
       				}
       				//Else continue key in record		
       				else{
       					entries = person[0].getEntries();//Get number of entries entered
       					for(j=0; j<entries; ++j){
       						
       						duplicate = person[j].equals(name, address, telNo, email);//Check for duplication of entries
       						
       						if(duplicate == true) {
       							Print.duplicateError();
       							--i;
       							--looping;
       							break;
       						}
       					}
       					if(duplicate == false)//else save record
       						person[entries] = new Person(name, address, telNo, email);
       				}	
    			}
    		}
    		
    		//Generate List of Record Entered
    		else if(optionMenu == 2) {
    			System.out.println("Generate List of Record Entered");
    			entries = person[0].getEntries();//Get number of record entered
    			
    			for(i=0; i<entries; ++i) {
    				System.out.println(i+1 + ". " + person[i].getName());
    			}

    			System.out.print("\nPerson no : ");
    			int searchPerson = input.nextInt();
    			searchPerson = searchPerson - 1;
    			//Print record searched
    			System.out.println("Name     : " + person[searchPerson].getName());
    			System.out.println("Address  : " + person[searchPerson].getAddress());
    			System.out.println("Phone No.: " + person[searchPerson].getTelNo());
    			System.out.println("Email    : " + person[searchPerson].getEmail());
    		}
    		
    		//Search Record by Name
    		else if(optionMenu == 3) {
    			System.out.println("Search Record by Name");
    			System.out.print("Name : ");
    			input.useDelimiter("\n");
    			String searchName = input.next();
    			
    			entries = person[0].getEntries();//Get entries of record entered
    			
    			for(i=0; i<entries; ++i) {
    				
    				boolean booleanName = person[i].searchName(searchName);//Search record
    				
    				if(booleanName == true) {//Print if record found
    					System.out.println("\nRecord no. " + (i+1));
    					System.out.println("Name     : " + person[i].getName());
    					System.out.println("Address  : " + person[i].getAddress());
    					System.out.println("Phone No.: " + person[i].getTelNo());
    					System.out.println("Email    : " + person[i].getEmail());
    					
    					result = true;//Track if the record is found or not
    				}
    				else
    					result = false;//Track if the record is found or not
    			}
    			
    			if(result == false)//Print if record not found
    			System.out.println("Record not found.");
    		}
    		
    		//Modifiy Record
    		else if(optionMenu == 4) {
    			System.out.print("Entry No. : ");
    			int searchEntry = input.nextInt();
    			
    			System.out.print("\n\nEnter Name      : ");
       			input.useDelimiter("\n");
       			String name = input.next();
       				
       			System.out.print("Enter Address      : ");
       			input.useDelimiter("\n");
       			String address = input.next();
       					
       			System.out.print("Enter Phone Number : ");
       			long telNo = input.nextLong();
       			
       			System.out.print("Enter Email        : ");
       			String email = input.next();
       			
       			entries = person[0].getEntries();//Retrieve record entered
       			//Check for duplication
       			for(i = 0; i<entries; ++i) {
       				duplicate = person[i].equals(name, address, telNo, email);
       			}
       			
       			if(duplicate == true)//print if duplicated record are found
       				Print.duplicateError();
       					
       			else//Save if it not a duplicated record
       				person[searchEntry-1].makeCopy(name, address, telNo, email);
    		}
    		//Exit program 
    		else if(optionMenu == 5)
    			break;
    		//Check for continue looping
    		System.out.print("Do you want to continue (Y/N)?");
			optionContinue = input.next();
			
			System.out.println();
    	}while(optionContinue.equals("N") == false && optionContinue.equals("n") == false);
    	
    	Print.createBy();
    }
    
}
  #3  
Old 11-Jul-2009, 14:16
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Please Check the difference between this 2 java programs


Post the stack dump.

All you really need to do, is trace the dump (from bottom-up) based on the line# and file name(s) that are given. That should pin-point the problem.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET, Java, or C++? ironspider Miscellaneous Programming Forum 1 21-Apr-2009 01:22
Turn JAVA progams into working programs Mattb10M Java Forum 1 01-Jan-2009 18:16
Scalability in Java and C++ agx Miscellaneous Programming Forum 7 04-Feb-2006 16:35
Need help understand function prototypes! Blstretch C++ Forum 20 25-Oct-2005 15:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:48.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.