-2

This is the 'Lead' class, when I try to call Leads.primeLead() , I get a "Non-static method cannot be referenced from a static context" error.

I do understand the error, but I do not understand why when I defined a constructor and initilized an object, I cannot apply the method primeLead() on object lead1 .

How do I solve this?

    import java.util.ArrayList;
import java.util.Scanner;

public class Lead extends Main{

    String nameLead;
    int ageLead;
    int phoneLead;
    String cityLead;
    String email;
    String otherNotes;

    int indexOfLead = 0;
    int i = indexOfLead;

    ArrayList<String> names = new ArrayList<String>();
    ArrayList<Integer> ages = new ArrayList<Integer>();
    ArrayList<Integer> phones = new ArrayList<Integer>();
    ArrayList<String> cities = new ArrayList<String>();
    ArrayList<String> emails = new ArrayList<String>();
    ArrayList<String> notes = new ArrayList<String>();


    Scanner leads = new Scanner(System.in);

    Lead(){
        i = 0;
        // Need to create an ArrayList that has all the Arraylists above.
       }

       Lead lead1 = new Lead();




   /* public mainMenuLead(){
        System.out.println("Please choose one of the following options");
    } */




    public static void primeLead(){
        i = 0;
    System.out.println("============================================");
    System.out.println("  Please enter by the following order : ");
    System.out.println("     Name, age, phone , city, mail     ");
    System.out.println("============================================");

    System.out.println("Please enter the name of the Lead :  ");
    names.add(leads.nextLine());

    System.out.println("Age? :  ");
    ages.add(Integer.parseInt(leads.nextLine()));

    System.out.println("Phone number? ");
    phones.add(Integer.parseInt(leads.nextLine()));

    System.out.println("Would you like to add ... ");
    System.out.println("1) City? ");
    System.out.println("2) Email? ");
    System.out.println("3) Notes?  ");

        if(leads.nextLine().equals("1")){
            System.out.println("Please add City: ");
            cities.add(leads.nextLine());

        } else if (leads.nextLine().equals("2")){
            System.out.println("Please add email : ");
            emails.add(leads.nextLine());

        } else if(leads.nextLine().equals("3")){
            System.out.println("Please add any other notes you may have:  ");
            notes.add(leads.nextLine());
        }






    }
}




    public void primeLead(){
        i = 0;
    System.out.println("============================================");
    System.out.println("  Please enter by the following order : ");
    System.out.println("     Name, age, phone , city, mail     ");
    System.out.println("============================================");

    System.out.println("Please enter the name of the Lead :  ");
    names.add(leads.nextLine());

    System.out.println("Age? :  ");
    ages.add(Integer.parseInt(leads.nextLine()));

    System.out.println("Phone number? ");
    phones.add(Integer.parseInt(leads.nextLine()));

    System.out.println("Would you like to add ... ");
    System.out.println("1) City? ");
    System.out.println("2) Email? ");
    System.out.println("3) Notes?  ");

        if(leads.nextLine().equals("1")){
            System.out.println("Please add City: ");
            cities.add(leads.nextLine());

        } else if (leads.nextLine().equals("2")){
            System.out.println("Please add email : ");
            emails.add(leads.nextLine());

        } else if(leads.nextLine().equals("3")){
            System.out.println("Please add any other notes you may have:  ");
            notes.add(leads.nextLine());
        }






    }
}

second file(Where Lead.primeLead() is called:

    import java.util.Scanner;


public class Main {
 boolean exit = false;



    public void runMenu(){
        printHeader();
        while(!exit){
            mainMenu();
            int choice = getInput();
            performAction(choice);
        }
    }

    private void performAction(int choice){
        switch(choice){
            case 1:
                new Lead();
                Lead.primeLead();
            case 2:
            case 3:
            case 4:
            case 5:
                exit = true;
                System.out.println("Bye!");
                break;
        }

    }
    public void printHeader(){
        System.out.println("===========================================");
        System.out.println("                  Hello user!              ");
        System.out.println("               Welcome to our lead         ");
        System.out.println("                 Management tool           ");
        System.out.println("===========================================");

    }

    public void mainMenu(){
        System.out.println("\nPlease select one of the following options: ");
        System.out.println("1) Create a new lead");
        System.out.println("2) View all the leads");
        System.out.println("3) Connect ");
        System.out.println("4) View statistics");
        System.out.println("5) Exit   ");




    }

    private int getInput(){ // Scanner takes input from user, returns his choice.
        Scanner kb = new Scanner(System.in);
        int choice = -1;
        while(choice < 0 || choice > 5){
            try{
                System.out.print("\nEnter your choice:  ");
                choice = Integer.parseInt(kb.nextLine()); // What is Integer.parseInt ? what is . next line ?
            }
            catch(NumberFormatException e){
                System.out.println("Invalid selection, please try again.");
            }
        }
        return choice;
    }


    public static void main(String[] args){

        Main menu = new Main();

        menu.runMenu();



    }
}
  • Where do you call `primeLead()`? – dan1st Jul 11 '20 at 05:59
  • First thing it is Lead.primeLead() not Leads.primeLead() . Also this is wrong because primeLead is not a static method and could not be called using ClassName. – Harmandeep Singh Kalsi Jul 11 '20 at 06:00
  • 2
    That code will never compile, since you have **unbalanced braces**. There are two `}` end-braces following the `Lead(){` beginning of the constructor, 3 and 8 lines down from there. The first end-brace ends the constructor and the second ends the class, regardless of the indentations attempting to indicate otherwise, which means that all the remaining source **code is outside of a class**. – Andreas Jul 11 '20 at 06:02
  • @dan1st I call it in the main class, which is in a separate file. – ThomasReiner Jul 11 '20 at 06:07
  • 1
    Then show us the code – dan1st Jul 11 '20 at 06:08
  • @HarmandeepSinghKalsi Maybe I dont understand the error then.. defining primeLead as static will not allow me to reference the ArrayList – ThomasReiner Jul 11 '20 at 06:09
  • @dan1st Done. also updated the original code with proper braces. – ThomasReiner Jul 11 '20 at 06:12

1 Answers1

1

You create a Lead object and do not use it:

new Lead();
Lead.primeLead();

Instead, you should use the lead object you created:

Lead lead=new Lead();
lead.primeLead();

If you call <ClassName>.<methodName>(<parameters>);, you call a static method that has nothing to do eith the object.

If you call <objectOfTheClass>.<methodName>(<parameters>);, you call a non-static method that is part of the object.

And, as @Andreas points out in the comments, every open curly brace needs to have a closing curley brace at the appropriate position any the other way round.

dan1st
  • 12,568
  • 8
  • 34
  • 67