Jump to content

non-static method in static context

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
needHelp0611

needHelp0611

    Newbie

  • Members
  • Pip
  • 6 posts
I want the method addCashier to be called in the main method, but I get an error when I try to do this because the main method is static. Is there anyway to get around this?

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;



public class Cashier implements Runnable {
    private static int theNextNum = 0;
    String theCashier;    
    private int uniqueNum;
    private String timeNow;
    private String orderNo;
    private String stringOrder;   
    static int numOfOrders = 0;
    OrderList a = new OrderList();
    String nameNumber;
    private OrderList oList;
    


Cashier(String aCashier, OrderList orderList)
{
    
        theCashier = aCashier;
        oList = orderList;
        uniqueNum = ++theNextNum;
        orderNo = String.valueOf(uniqueNum);
        nameNumber = "Cashier: " +  theCashier + " Order: " + orderNo;
        numOfOrders ++;
}


 
public void addCashier()
{
        Cashier a = new Cashier("Bob", oList);
        Cashier b = new Cashier("Jim", oList);
        Cashier c = new Cashier("Jan", oList);
        Cashier d = new Cashier("Rob", oList);
     
        new Thread(a).start();
        new Thread(b).start();
        new Thread(c).start();
        new Thread(d).start();

}
     
    
    
    public String getTime()
    {
        String DATE_FORMAT_NOW = "HH:mm:ss";
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        String time = sdf.format(cal.getTime());
        timeNow = time; 
        return timeNow;
    }

    
    
 
    public static void main(String[] args) 
   {
   
           }

           
           
   public void run()
   {
       {
       try{
           Random randomNum = new Random();
           int max = 5000;
           int random = randomNum.nextInt(max); 
           Thread.sleep(random);
          
        }
 
      catch (Exception e)
{

}       
        timeNow = getTime();
        stringOrder =  nameNumber + " - " + timeNow ;
        oList.addOrder(stringOrder);   
        System.out.println(stringOrder);
        
        
               
    } }
}

Edited by Alexander, 20 October 2010 - 04:30 AM.
Please add [code] tags around your code


#2
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
That's because Main() is a static method, and addCashier is not. You have to make an instance of your class using Main(), and call the method using the instance as an identifier. For example...

    public static void main(String[] args) 

   {

   Cashier instanceOfCashier = new Cashier;

   instanceOfCashier.addCashier();

           }


...you don't have to call it instanceOfCashier, you can call it what you like :)

Hope this helps!

EDIT: To create a new class instance, you use

className instanceName = new className(constructorArguments[])
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)

#3
needHelp0611

needHelp0611

    Newbie

  • Members
  • Pip
  • 6 posts
Thanks for your help but when I add:

public static void main(String[] args)
{
Cashier instance = new Cashier();
instance.addCashier();


}

I get - java.lang.NullPointerException, is it something else in my code? I know my addCashier() method works, I just don't know who to correctly call it.

Is it correct to create a new cashier and then call a method from that that creates new cashiers?

Any help is much appreciated!

#4
needHelp0611

needHelp0611

    Newbie

  • Members
  • Pip
  • 6 posts
Sorry didn't read your last comment, I now have:

public static void main(String[] args)
{
Cashier a = new Cashier("Bob", oList);
Cashier b = new Cashier("Jim", oList);
Cashier c = new Cashier("Jan", oList);
Cashier d = new Cashier("Rob", oList);

new Thread(a).start();
new Thread(b).start();
new Thread©.start();
new Thread(d).start();

}

But I am still getting java.lang.NullPointerException, any ideas?

#5
Metalhead

Metalhead

    Newbie

  • Members
  • PipPip
  • 27 posts
Normally you don't want to create an instance of Cashier to create 4 other instances of Cashier.
My guess is that you want to make your method static, so you can call it from main;
public static void addCashier() {
    ...
}

public static void main(String[] args) {
       addCashier();
}

Somewhere you will also have to create the oList instances.
Maybe something like
Cashier a = new Cashier("Bob", new OrderList());
etc...