Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

help with java method

  • 23-03-2004 05:33PM
    #1
    Closed Accounts Posts: 1,152 ✭✭✭


    hey folks im having a probel with this bit of code im tryin to do.

    i get an error mesage:



    non-static method createAccount() cannot be referenced from a static context
    createAccount ();

    when i execute this following bit of code:
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class Atm extends JFrame
    {
    	JPanel cardHolder;
    	JLabel labelOne, labelTwo;
    	
    	//declare stream objects
    
            FileOutputStream outputAccount;
            ObjectOutputStream objSaveAccount; //stream to save an object
            
            FileInputStream inputAccount;
            ObjectInputStream objGetAccount;
    
    
    
    	CardLayout card;
    	Login panel1;
    	Menu panel2;
    	withdrawal panel3;
    	lodge panel4;
    	topUp panel5;
    
    
    
    	public Atm()
    	{
    		super("Welcome to Irish National Bank ATM System");
    		Container c = this.getContentPane();
    		c.setLayout(new FlowLayout());
    		c.setBackground(Color.white);
    
    
    		cardHolder = new JPanel();
    		card = new CardLayout();
    		cardHolder.setLayout(card);
    		cardHolder.setBackground(Color.white);
    
    		panel1 = new Login(this);
    		panel2 = new Menu(this);
    		panel3 = new withdrawal(this);
    		panel4 = new lodge(this);
    		panel5 = new topUp(this);
    
    
    		cardHolder.add(panel1, "panel1", 0);
    		cardHolder.add(panel2, "panel2", 1);
    		cardHolder.add(panel3, "panel3", 2);
    		cardHolder.add(panel4, "panel4", 3);
    		cardHolder.add(panel5, "panel5", 4);
    		
    		c.add(cardHolder);
    
    
    		setSize(500,400);
    		show();
    		
    		setVisible(true);
    
    	}
    
            public void changePanel(String in)
            {
                   card.show(cardHolder, in);
            }
    
    
    	public static void main(String args[])
    	{
    		Atm app = new Atm();
    		createAccount ();
    
    
    
    	}
    	
    
    	public void openOutputStream()
    	{
                try
                {
                	//create file and object output streams
                	outputAccount = new FileOutputStream("Account.txt");
                	objSaveAccount = new ObjectOutputStream(outputAccount);
                }
                
                catch(Exception error)
                {
                	System.err.println("Error opening file");
                	
                }
    
    	}
    	
    	
    	public void closeOutputStream()
    	{
    		try
    		{
    			objSaveAccount.close(); //close the object output stream
    			outputAccount.close(); //close the file output stream
    		}
    		
    		catch(IOException error)
    		{
    			System.out.println("Error closing file");
    		}
    		
    	}
    	
    	
    	public void openInputStream()
    	{
    	   try
    	   {
    		//create fiel and object input streams
    		inputAccount = new FileInputStream("Account.txt");
    		objGetAccount = new ObjectInputStream(inputAccount);
    		
    	   }
    	   
    	   catch(Exception error)
    	   {
    	   	System.out.println("Error opening file");
    	   }
               
               
             }
             
             public void closeInputStream()
             {
             	try
             	{
             		objGetAccount.close(); //close the object input stream
             		inputAccount.close(); //close the file input stream
    
             	}
             	
             	catch(IOException error)
             	{
             		System.err.println("Error closing file");
             	}
             	
             }
             
             
            public void createAccount()
            {
             	
             	Account a =  new Account(500,"1234","123456789");
    
             	try
             	{
                      objSaveAccount.writeObject((Object)a);
             	
                    }
                    
                    catch(IOException error)
                    {
                    	System.err.println("Error opening file");
                    }
    
    
              }
    
    
    }//end class
    
    
    

    any help much appreciated


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    If your writing a class with a main method, all other methods within the class must be static. Your only alternative is to move them into the ATM class.


  • Closed Accounts Posts: 1,152 ✭✭✭sound_wave


    thanks doodle! that got rid of that error..but im gettin another error or errors as the case may be

    > Executing: C:\Program Files\ConTEXT\ConExec.exe "C:\j2sdk1.4.1_04\bin\java.exe" Atm

    java.lang.NullPointerException
    at Atm.createAccount(Atm.java:155)
    at Login.<init>(Login.java:98)
    at Atm.<init>(Atm.java:43)
    at Atm.main(Atm.java:74)
    Exception in thread "main"
    > Execution finished.


    what does this mean?


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Ok, the problem here is that your instansiating your ObjectOutputStream in openOutputStream() and trying to access it in createAccount(). You can't do this because when you instansiate an object in a method, you can only use it in that method. Its called local scope.

    To overcome this, you either need to instansiate the object when you declare it at the head of the class, or just before you use it in the same method.


  • Closed Accounts Posts: 7 dicey


    Originally posted by sound_wave
    hey folks im having a probel with this bit of code im tryin to do.

    i get an error mesage:



    non-static method createAccount() cannot be referenced from a static context
    createAccount ();

    when i execute this following bit of code:

    	public static void main(String args[])
    	{
    		Atm app = new Atm();
    		createAccount ();
    	}
    

    any help much appreciated

    The problem is that createAccount is an instance method, not static - you need to change your main to
    	public static void main(String args[])
    	{
    		Atm app = new Atm();
    		app.createAccount ();
    	}
    


Advertisement