Hi every body ..

i got these problems with my code

when ever i choose "no" as an answer .. it requires me to create an a count while i don't want to create one ..

another problem occurs when i use the Acounte_Number to be the ID for the user .. which is a private variable in another class ..

like this ..

class Acount
{	
     static String name;
     static int ID;
	 private  int Account_Number;
     static double amount;
	 public double balance;

     static  Acount S[]=new Acount[100];
    
     public Acount(String name, int ID , int Account_Number,double amount,double balance  )
    	  {
    	  	this.Account_Number=Account_Number;
    	  	this.amount=amount;
    	  	this.balance=balance;
    	  }

//*********************************************************************	

    	  
 public int  getAccount_Number()
  {
    return Account_Number;
  }
  public void setAccount_Number(int x)
  {
  	Account_Number=x;
  }

when ever i used it an exception occur .. something else the Acount_Number dose not increase ..

how can i fix that ..

thanx for any replay ..

public   void creatAccounts(String name,String Gender, int Age, int ID,double balance,Adress AD,Acount AC )
    {
       
        String s="yes";
        int i=0;
        AC.setAccountNumber(i);
    
    	
    

    	 array [size]= new client(  name, Gender, Age, ID, balance, AD, AC);
    	 
    	 
    	 s=JOptionPane.showInputDialog("Do you Want to Creat New Acount ?(yes/no)","CREAT_ACOUNT");

    	
    	if(s!="no")
         	{
         			while(size!=100)
    	             {
    	             	 if(AC.getAccountNumber()<array.length)
	                     	{
	               	            CREAT(AC.getAcount_Number)
	               	            JOptionPane.showMessageDialog(null,"creatAccounts Successfully");
	             	        }
	          
                   	              
                   	              size++;
                   	              Acount.setAcount_Number(i++);

                   	              break;
                     }
                     

                           JOptionPane.showMessageDialog(null,"ID : "+ID);
         	}
              
            else
             {JOptionPane.showMessageDialog(null,"Full Array Max(100)");}
   

  }

}

Recommended Answers

All 30 Replies

if(s!="no")

You can't compare the contents of two Strings with == or != That tests for them being exactly the same Object. To test for two Strings having the same sequence of characters in them use
s.equals("no")
or
s.equalsIgnoreCase("no").. which may be better for testing user-typed input.

no replay for the other problems .. !!

when ever i used it an exception occur

Please copy and paste here the FULL text of any error messages.

OK ..

--------------------Configuration: JavaProject - JDK version 1.6.0 <Default> - <Default>--------------------
Exception in thread "main" java.lang.NullPointerException
at client.creatAccounts(person.java:112)
at TEST.main(person.java:393)

public   void creatAccounts(String name,String Gender, int Age, int ID,double balance,Adress AD,Acount AC )
    {
       
        String s="yes";
        int i=0;
        AC.setAccount_Number(i); // this is line 112 where dose the error occurs
    
    	
    

    	 array [size]= new client(  name, Gender, Age, ID, balance, AD, AC);
    	 
    	 
    	 s=JOptionPane.showInputDialog("Do you Want to Creat New Acount ?(yes/no)","CREAT_ACOUNT");

    	
    	if(s.equalsIgnoreCase("yes"))
         	{
         			while(size<array.length)
    	             {
    	             	 if(AC.getAccount_Number()<array.length)
	                     	{
	               	            client.CREAT();
	               	            JOptionPane.showMessageDialog(null,"creatAccounts Successfully");
	             	        }
	          
                   	              
                   	              size++;
                   	              AC.setAccount_Number(i++);

                   	              break;
                     }
                     

                           JOptionPane.showMessageDialog(null,"ID : "+ID);
         	}
              
            else
             {JOptionPane.showMessageDialog(null,"Full Array Max(100)");}
   

  }

}

Is the AC variable null? That would cause the NullPointerException.
Add a println to show its value if you don't know for sure.

mm

AC represent an object of Acount class where dose the Acount_Number is defined as private ..

i don't get it .. how it could be NULL

Add a println to show its value if you don't know for sure.

the problems is that the code won't execute while i used

AC.Acount_Number(0);

Don't know ..!

What was the value printed for AC when you print it just before line 112?

mmm .. if i exclude AC.setAcount_number from me code another problem occurs with AC.getAcount_number()


am gona insert the hole code here ..

import javax.swing.*;
import java.util.*;
interface Bank
{
	public abstract void print();
}
//*************************************************************************************************************************************

  abstract class person implements Bank
{
	public String name;
	public String Gender;
	public int Age;
	 
	public person(String name, String Gender, int Age)
	{
		this.name=name;
		this.Gender=Gender;
		this.Age=Age;
	}
	
	public  void print()
	{
		JOptionPane.showMessageDialog(null,"Name : "+name);
		JOptionPane.showMessageDialog(null,"Gender : "+Gender);
		JOptionPane.showMessageDialog(null,"Age : "+Age);
	}
	

}

//*************************************************************************************************************************************

class client extends person 
{

	
	static   int ID;
	public Adress AD;
	public Acount AC;
    client array[]=new client[100];
    static int size=0;
    public double balance;
    
    public client(String name,String Gender, int Age, int ID, double balance, Adress AD,Acount AC)	 // default constrctor		
	{
		super(name,Gender,Age);
		this.ID=ID;
	   this.balance=balance; 

	    this.AD=AD;

	    this.AC=AC;

				
	}


//********************************************************************************	
	
    	
public static void CREAT()
	{

		String name;
		String Gender;
		int Age;	 	
		String street;
		String city;
		String contry;
		int Account_Number=0;
		double amount;
		double balance;
		Acount AC;
		Adress AD; 
			

	    String x = JOptionPane.showInputDialog("Enter your name:");

		 x = JOptionPane.showInputDialog ("Enter your Gender:(m/f)");
		
		int y = Integer.parseInt(JOptionPane.showInputDialog("Enter your Age:"));

		 
		 x = JOptionPane.showInputDialog("enter your Street:");
		 
		 x = JOptionPane.showInputDialog("enter your City:");
		 
		 x = JOptionPane.showInputDialog("enter your Contry:");
		
	}
	//******************************//




public void array(){
	
	
for(int i=0;i<array.length;i++)
{
array[i]=new client(name, Gender, Age, ID, balance, AD, AC);
}
}


public   void creatAccounts(String name,String Gender, int Age, int ID,double balance,Adress AD,Acount AC )
    {
       
        String s="yes";
        int i=0;
        AC.setAccount_Number(i);
    
    	
    

    	 array [size]= new client(  name, Gender, Age, ID, balance, AD, AC);
    	 
    	 
    	 s=JOptionPane.showInputDialog("Do you Want to Creat New Acount ?(yes/no)","CREAT_ACOUNT");

    	
    	if(s.equalsIgnoreCase("yes"))
         	{
         			while(size<array.length)
    	             {
    	             	 if(AC.getAccount_Number()<array.length)
	                     	{
	               	            client.CREAT();
	               	            JOptionPane.showMessageDialog(null,"creatAccounts Successfully");
	             	        }
	          
                   	              
                   	              size++;
                   	              AC.setAccount_Number(i++);

                   	              break;
                     }
                     

                           JOptionPane.showMessageDialog(null,"ID : "+ID);
         	}
              
            else
             {JOptionPane.showMessageDialog(null,"Full Array Max(100)");}
   

  }

}


//******************************************************************************************************************


	
 class Acount
{	
     static String name;
     static int ID;
	 private  int Account_Number;
     static double amount;
	 public double balance;

     static  Acount S[]=new Acount[100];
    
     public Acount(String name, int ID , int Account_Number,double amount,double balance  )
    	  {
    	  	this.Account_Number=Account_Number;
    	  	this.amount=amount;
    	  	this.balance=balance;
    	  }

//*********************************************************************	

    	  
 public int  getAccount_Number()
  {
    return Account_Number;
  }
  public void setAccount_Number(int x)
  {
  	Account_Number=x;
  }
  
  
//*********************************************************************	


 public static int search(int Account_Number) // search method by Acount No
    {
    	int s=-1;
    	
      
    	for(int i=0;i<S.length;i++)
    	{
    		if(S[i]!=null && S[i].Account_Number==Account_Number)
    		{
    			s=i;	
    		}
    	
        }
    			
    return s;
    }
//**************************************************************************

 public static void  searchAc(int Account_Number) // search method by Acount No    
    
 {
 	
    	int s= Integer.parseInt(JOptionPane.showInputDialog("Enter your AcountNO : "));
      
    	for(int i=0;i<S.length;i++)
    	{
    		if(S[i]!=null && S[i].Account_Number==Account_Number)
    		{
    			s=i;
    			JOptionPane.showMessageDialog(null,"FOUND" +S[i].Account_Number);
    				
    		}
    		
    		else
    		{JOptionPane.showMessageDialog(null,"NOT FOUND");}
    		
    		 break;
    			
    	
        }
    			
    
 }
 
 //**********************************************************************************************************
  public static void  searchID(int Account_Number) // search method by ID    
    
 {
 	
    	int s= Integer.parseInt(JOptionPane.showInputDialog("Enter your ID : "));
      
    	for(int i=0;i<S.length;i++)
    	{
    		if(S[i]!=null && S[i].ID==ID)
    		{
    			s=i;
    			JOptionPane.showMessageDialog(null,"FOUND" +S[i].ID);
    				
    		}
    		
    		else
    		{JOptionPane.showMessageDialog(null,"NOT FOUND");}
    		
    		 break;
    			
    	
        }
    			
    
 }
 //**********************************************************************************************************
  public static void  searchName(int Account_Number) // search method by Name ..    
    
 {
 	
    	String s=(JOptionPane.showInputDialog("Enter your Name: "));
      
    	for(int i=0;i<S.length;i++)
    	{
    		if(S[i]!=null && S[i].name==name)
    		{
    			
    			JOptionPane.showMessageDialog(null,"FOUND" +S[i].name);
    				
    		}
    		
    		else
    		    {JOptionPane.showMessageDialog(null,"NOT FOUND");}
    		
    	    break;		
 
        }
       
    			
    
 }
  
//*********************************************************************	
    
    public static void Depost(double  de,int   Account_Number)
    {
    	
    	 int ACO= Integer.parseInt(JOptionPane.showInputDialog("Enter your AcountNO : "));
    	 int  x =search(ACO);
    	 
    	 if(x!=-1)
    	{
    		
    	    de= Integer.parseInt(JOptionPane.showInputDialog("Enter the amount you want to depoait .."));
            amount=amount+de;
        
    	    JOptionPane.showMessageDialog(null,"the depost balans:"+amount);
        }
        
        else 
        	JOptionPane.showMessageDialog(null,"NOT FOUND ..");
    }
   
    
//*********************************************************************


public static void withdraw(double  wid,int Account_Number)
    {
    	
        int ACO = Integer.parseInt(JOptionPane.showInputDialog("Enter your Acount NO."));
    	int x=search(ACO);
    	
    	if(x!=-1)
    	{
    	
    	 wid= Integer.parseInt(JOptionPane.showInputDialog("Enter the amount you want to widthrol .."));
         amount=amount-wid;
    	JOptionPane.showMessageDialog(null,"the withdraw balance:"+amount);
    	
        }
        
        else 
        	JOptionPane.showMessageDialog(null,"NOT FOUND ..");
     } 
      
//************************************************************************

 public static void Balance()
    {
    	JOptionPane.showMessageDialog(null,"the amount is:"+amount);
    }
}

//***********************************************************************************************************************        

class Adress
{
	
	String street;
	String city;
	String contry;

	public Adress(String street,String city,String contry)
	{
		this.street=street;
		this.city=city;
		this.contry=contry;

	}
	public void print()
	{
		
		System.out.println ("Contry name:"+contry);
		System.out.println ("City name:"+city);
		System.out.println ("Street name:"+street);
	}
}


//*************************************************************************************************************************************


class TEST
{
	
     static  double amount;
     static String name;
     static Adress AD;
     static Acount AC;
     static double  balance, Dep, Wid;
     static  String Gender;
     static int ID,Age, cv, xxx, xx;
     
     
public static void main (String[] args)
{

	
	client clit = new client(name, Gender, Age, ID, balance, AD, AC);
	
	do
	{
		int x = Integer.parseInt(JOptionPane.showInputDialog(null," 1- creat new Account \n 2- depost \n 3- withdrawal \n 4- Balance \n 5- Search \n 6- save\n 7- Exit \n Enter your choice search: "));
        
        
        switch(x)
        {
        	case 1 : clit.creatAccounts( name, Gender,  Age,  ID, balance, AD, AC ); break;
        	
        	case 2 : 

        	       	 Acount.Depost(Dep,xx);
                     Acount.Balance(); break;
                
        	case 3 :
        		     
        			 
        	       	 Acount.withdraw(Wid,xxx);
                     Acount.Balance();
                     
                      break;
                     
              
            case 4 : Acount.Balance(); break;
            
            case 5 :
                     int y=Integer.parseInt(JOptionPane.showInputDialog(" 1-By Account number\n 2- By Name \n 3- By ID \n Enter your choice search:"));
          		
                 	switch(y)
             	     {
             		   case 1:Acount.searchAc(cv); break;
             		   case 2:Acount.searchName(cv); break;
             		   case 3:Acount.searchID(cv); break;
             	     }
             	     
             	     break;
             	     
           case 6 :break;
           case 7: System.exit(0); break;
             
    	}
	}
	while(true);
           
	
  }

}

another problem occurs with AC.getAcount_number()

You MUST post the full text of the error messages you get.

What was the value printed for AC when you print it just before line 112?

This is the fourth time (and the last) I asked this question. If you refuse to follow my requests for info about how your program is executing, I'll have to give up trying to help.

OK .. i did what you asked me to do

JOptionPane.showMessageDialog(null,+AC.getAccount_Number());

the same problem just show up ..

i know that am so bossy, but really i need some help

am sorry ..

and i did that

JOptionPane.showMessageDialog(null,+AC.getAccount_Number());

before the line 112 ..

Can you read and understand English? I asked this:
Add a println to show its value if you don't know for sure.
Your calls to showMessageDialog does not do that.

Try this:
System.out.println("AC=" + AC); // Show value of the AC variable

--Configuration: JavaProject - JDK version 1.6.0 <Default> - <Default>--------------------
AC=null
Exception in thread "main" java.lang.NullPointerException
at client.creatAccounts(person.java:128)
at TEST.main(person.java:394)

Process completed.


that's what i've got ..

The printout shows that AC is null. It needs to have a value.

Next question: Where do you assign a value to the argument that is passed to the AC variable? Check backwards from the call to createAccounts and find where you need to assign a value to that argument.

@NormR1 he doesn't just asked him few minutes ago why did he forwarded to me automated message about his post edited and asked him to read FAQ & rules and of course he send me back my message.
BTW error message is

Exception in thread "main" java.lang.NullPointerException
	at Client.creatAccounts(QuickCoding.java:102)
	at QuickCoding.main(QuickCoding.java:348)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)

@Nirmeen Ased, you getting that message due to call from line 348. Here you are calling create account method with some set of parameters. The problem is that you did declare them as

static double amount;
    static String name;
    static Address AD;
    static Account AC;
    static double balance, Dep, Wid;
    static String Gender;
    static int ID, Age, cv, xxx, xx;

but none of them been actually initialized. Therefore when you try to set account array to position zero you get exception. You failed to initialized.

thank you so much NormR1 and peter_budo

the code executed so well but Acount_Number won't increased ..

i'll try with it ..

Acount_Number won't increase

When you have a problem you should show what the output is and describe why it is not what you want it to be and show what you want the output to be.
Also showing the code where the problem is can help.

Add some printlns to your code that prints the value of Account_Number every time that Account_Number is changed and used. The printed output should help you find where the problem is.

If the value is always zero and you don't change it to a value other than zero, it will remain zero.

Did you read my instructions?
Add some printlns to your code that prints the value of Account_Number every time that Account_Number is changed and used.

You post says you only printed it in two places:

i put a println befor the end of the loop and after its end

Put a println after EVERY PLACE YOU change its value!!!

i posted them at the places that may its value change there ..

i give acount_number initial value 0 but then

AC.setAcount_Number(i++)

i want it to increase whenever i creat a new a count ..

i've read your instructions so well and they were so helpful ..

You need to debug your code to figure out why it is not doing what you want it to do.
One way is how I'm telling you:

What is printed out when you added println statements after ALL the places where the value of Account_Number was changed?

For example:

Account_Number = x;
  System.out.println("Acct_Nbr1=" + Account_Number);  // show new value

Glad you figured it out.

Just don't forget to remove them once you done with debugging (anyway any decent IDE IntelliJ, Eclipse, NetBeans even JCreator allows you to run in debug where you can setup break points and check what variables are holding as values).
Also you should consider to use ArrayList or List instead of simple array, then you do not need to worry about incrementing position value. Secondly you should think about objects ownership, and if you really need to pass all objects as you do for start in main method

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.