Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: NullPointerException

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointerException

    My assignment is about creating a payroll program.Compiler can compile the program and able to run it,however whenever i enter an email address it produce an error.I've been trying to look for the mistakes but couldnt find it.

    Here is the error in command prompt:

    Exception in thread "main" java.lang.NullPointerException
     
                       at Employee.validateEmail(employee.java:132)
                       at Employee.empdetails(employee.java:110)
                       at Employee.main(employee.java:40)

    source code

    import javax.swing.*;
     
     
    class Employee{
     
    static String details;
    static	String name[]= new String [2];
    static	int  dobday[]= new int[2];
    static	int  dobmth[]= new int[2];
    static	int  dobyear[]= new int[2];
    static	String ic[]= new String[2];
    static	int  adday[]= new int[2];
    static	int  admth[]= new int[2];
    static	int  adyear[]= new int[2];
    static	double  bsalary[]= new double[2];
    static	String  email[]= new String [2];
    static	String  position[]= new String [2];
    static	String  mstatus[]= new String [2];
     
    static String z[] = new String[2];
    static String z1[] = new String[2];
     
    static int dd2;
    static int mm2;
    static int yyyy2;
    static int years[]=new int[2];
     
    static double nyallowance[]=new double[2];
    static double mrallowance[]=new double[2];
    static double costol;
    static double netsalary[]=new double[2];
    static double epf[]=new double[2];
    static String marrystatus;
     
    static String emp[]=new String[2];
     
    	public static void main(String payroll[]){
     
    	empdetails ();
    	empid();
    	wduration();
    	netsalary();
    	result();
     
    	}
     
    	static void empdetails(){
     
    	String details;
    	int a,b,c,x,j;
     
    	for(int k=0;k<2;k++){
     
    	name[k]=JOptionPane.showInputDialog("Please Enter Your Full Name");
     
    	details=JOptionPane.showInputDialog("Enter your date of birth:DAY");
    	dobday[k]=Integer.parseInt(details);
     
     
    	if(dobday[k]>31 && dobday[k]<1){
     
    		JOptionPane.showConfirmDialog(null,"Wrong Date, Please enter correct date");
     
    	}
     
     
    	details=JOptionPane.showInputDialog("Enter your date of birth:MONTH");
    	dobmth[k]=Integer.parseInt(details);
     
     
    	if(dobmth[k]>12 && dobmth[k]<1){
     
    		JOptionPane.showConfirmDialog(null,"Wrong Month, Please enter correct Month");
    	}
     
     
     
    	details=JOptionPane.showInputDialog("Enter your date of birth:YEAR");
    	dobyear[k]=Integer.parseInt(details);
     
    	ic[k]=JOptionPane.showInputDialog("Enter your Identification Number");
     
    	details=JOptionPane.showInputDialog("Enter your appointed date:DAY");
    	adday[k]=Integer.parseInt(details);
     
    	if(adday[k]>31 && adday[k]<1){
     
    		JOptionPane.showConfirmDialog(null,"Wrong Date, Please enter correct date");
    	}
     
    	details=JOptionPane.showInputDialog("Enter your appointed date:MONTH/(in number not word/)");
    	admth[k]=Integer.parseInt(details);
     
    	if(admth[k]>12 && admth[k]<1){
     
    		JOptionPane.showConfirmDialog(null,"Wrong Month, Please enter correct Month");
     
    	}
     
     
    	details=JOptionPane.showInputDialog("Enter your appointed date:YEAR");
    	adyear[k]=Integer.parseInt(details);
     
    	details=JOptionPane.showInputDialog("Enter your basic salary");
    	bsalary[k]=Double.parseDouble(details);
     
    	details=JOptionPane.showInputDialog("Enter your email address");
     
    	validateEmail();
    	email[k]=details;
     
     
    	position[k]=JOptionPane.showInputDialog("Enter your Position Title");
     
    	mstatus[k]=JOptionPane.showInputDialog("Enter your marital status:Married or Not Marry");
     
    	z[k]=dobday[k]+"//"+dobmth[k]+"//"+dobyear[k];
    	z1[k]=adday[k]+"//"+admth[k]+"//"+adyear[k];
    	dd2=adday[k];
    	mm2=admth[k];
    	yyyy2=adyear[k];
     
    	marrystatus=mstatus[k];
    	}
    	}
     
     
     
    	static void validateEmail(){
     
    	int h=details.indexOf('@');
    	int i=details.lastIndexOf('.');
    	int j=details.length();
     
     
    	if(h>0){
    			if(i!=-1 && i!=(j-1)){
    				if(h<i && 1<(i-h)){
     
    					JOptionPane.showMessageDialog(null,"Valid email");
    				}
    					else{
     
    					JOptionPane.showMessageDialog(null,"Invalid email");
    					}
    				}
     
    					else{
     
    					JOptionPane.showMessageDialog(null,"Invalid email");
    					}
    				}
    					else{
     
    					JOptionPane.showMessageDialog(null,"Invalid email");
    					}
    	}
     
    	static void empid(){
     
    		String id,num;
     
    	int x=0;
     
     
     
     
    	for(int i=0; i<2; i++){
     
    	if (i<20){
    	id="0";
     
    	}
    	else{
    	id="";
    	}
     
    	if(i>1){
    	num="";
    	}
    	else{
    	num="0";
    	}
     
    	if(i>0){
    	x=x+5;
    	}
    	else{
    	x=0;
    	}
     
    		emp[i]=id+num+x;
     
    		System.out.println("EMP"+ emp[i]);
     
     
    	}
    	}
     
     
    	static void wduration(){
     
    	int dd1,mm1,yyyy1,years;
     
     
    	dd1 = 11;
    	mm1 = 11;
    	yyyy1 = 2011;
     
    	if(yyyy1==yyyy2){
    	if(yyyy1>yyyy2){
     
    	years=0;
    	}
    	}
    	if (mm1>mm2){
    	years=yyyy1-yyyy2;
    	}
     
    	else{
     
    	if (mm1<mm2){
    	years=yyyy1-yyyy2-1;
    	}
     
    	else{
     
    	if (mm1==mm2){
     
    	if (dd1>dd2){
    	years=yyyy1-yyyy2;
    	}
    	if (dd1<dd2){
    	years=yyyy1-yyyy2-1;
    	}
    	if (dd1==dd2){
    	years=yyyy1-yyyy2;
    	}
    	}
     
    	}
     
    	}
     
     
     
    }
     
     
    	static void netsalary(){
     
    	//formula Net salary = basic salary+new year allowance+marital allowance+cosf of living - EPF
     
     
    	for(int h=0;h<2;h++){
     
    	if (bsalary[h]<=10000){
     
    	nyallowance[h]=2000;
     
    	}
     
    	else{
     
    	nyallowance[h]=0;
     
    	}
     
    	if (bsalary[h]>10000 && bsalary[h]<20000){
     
    	nyallowance[h]=3000;
     
    	}
     
    	else{
     
    	nyallowance[h]=0;
     
    	}
     
    	if (bsalary[h]>20000){
     
    	nyallowance[h]=5000;
     
    	}
     
    	else{
     
    	nyallowance[h]=0;
     
    	}
     
    	if (marrystatus.equals("Married")){
     
    	mrallowance[h]=4000;
     
    	}
     
    	else{
     
    	mrallowance[h]=0;
     
    	}
     
    	epf[h]= bsalary[h]*.4;
     
    	costol=4500;
     
    	netsalary[h]=bsalary[h]+nyallowance[h]+mrallowance[h]+costol+epf[h];
     
    }
    }
     
     
    	static void result(){
     
    	for(int n=0;n<2;n++){
     
    	System.out.println("Employee ID			:"+emp[n]);
    	System.out.println("Name                :"+name[n]);
    	System.out.println("Date of Birth       :"+z[n]);
    	System.out.println("IC Number           :"+ic[n]);
    	System.out.println("Appointed Date      :"+z1[n]);
    	System.out.println("Working Duration    :"+years[n]+"Years");
    	System.out.println("Position			:"+position[n]);
    	System.out.println("Marital Status      :"+mstatus[n]);
    	System.out.println("Email               :"+email[n]);
    	System.out.println();
    	System.out.println("Pay Sheet ---------------------------------");
    	System.out.println("\n");
    	System.out.println("Basic Salary		:"+bsalary[n]);
    	System.out.println("EPF									:"+epf[n]);
    	System.out.println("Cost of Living		:"+costol);
    	System.out.println("Marital Allowance	:"+mrallowance);
    	System.out.println("New Year Allowance	:"+nyallowance);
    	System.out.println();
    	System.out.println("--------------------------------------------");
    	System.out.println("Netsalary: "+netsalary);
    	System.out.println("--------------------------------------------");
    	}
    	}
     
     
     
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: NullPointerException

    That is waaay too much code for us to wade through. Please shorten it to an SSCCE that demonstrates the problem in as few lines as possible.

    Did you google the exception? A NPE is thrown when you attempt to dereference a null reference. This can happen when you try to use a variable that hasn't been set to reference an instance. The stack trace even tells you exactly what line the problem occurs on. Hint: when do you initialize the variable you're trying to use? Add some print statements, or better yet step through this with a debugger, to figure it out.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException

    Someone might be able to help you if you tell us what lines 132,110 and 42 are in your program.

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException

    I think that I have figured out what's wrong with your code. You have globally declared a String details object and also another String details object in empdetails() which would hide the global String. And in your program, you are storing the values received from the user in details ( the one declared in empdetials() ) and so the String details declared globally has nothing stored in it. In another method, you are trying to access the Strin details object which isn't initialised yet ( no String value is stored ) Therefore a NullPointerException is thrown. To correct it, simply remove String details varibale from empdetails()
    static void empdetails(){
     
    	// delete this String details;
    	int a,b,c,x,j;

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: NullPointerException

    Surprise that we ditched into the one month old topic. Anyway, null pointer exception is the most popular runtime error in Java programming, so it should be taken with care.

Similar Threads

  1. [SOLVED] NullPointerException
    By macko in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 21st, 2011, 11:35 AM
  2. NullPointerException ... and does it work?
    By thebestpearl in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2011, 01:32 PM
  3. [SOLVED] Nullpointerexception
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2010, 10:33 PM
  4. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  5. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM