1

I get the null value whenever I use showMessage(); I believe that it is because of me returning c at the end of the method. Are there any way to print out getName() without it being in main method, as in getName() being in the showMessage() method. . Here is my Candidate class

public class Candidate {

private String name;
private int votes;

//default constructor
public Candidate() {

String name = "Not Available! ";
int votes = 0; 
}

//overloaded constructor
public Candidate(String _name, int _votes){
  name  = _name;
  votes = _votes;

}

//getter
public String getName(){return name;}
public int getVotes(){return votes;}

//setter

public void incrementVote(){
  votes = votes + 1;

}

public void setName(String _name){
    name =_name;
}

public void print(){

    System.out.println("To vote for " + name);


}
}

Here is my main

import java.util.*;

public class Election {
public static void main(String args[]){

System.out.println("Welcome to the polls! ");

 Candidate c1 = new Candidate();
 Candidate c2 = new Candidate();
 Candidate c3 = new Candidate();
 Candidate c4 = new Candidate();


c1 = inputCandidate();
c2 = inputCandidate();
c3 = inputCandidate();
c4 = inputCandidate();



c1 = showMessage();

}

private static Candidate inputCandidate(){
    Scanner sc = new Scanner(System.in);
    String inputN;

    Candidate c = new Candidate();

    System.out.println("Enter candidate");

    inputN = sc.nextLine();
    c.setName(inputN);

   return c;
}

private static Candidate showMessage(){
    Candidate c = new Candidate();

    System.out.println(c.getName());

    return c;
}


}
6
  • 1
    showMessage printing null for the name is not possible. are you sure you re-compiled your code after altering it? or is it after that method that somewhere it prints null? Commented Oct 5, 2018 at 13:43
  • @Stultuske he does create a new Candidate object in showMessage() instead of using the ones he creates in the methods inputCandidate. So it should be very possible that it prints null as a name is never set in that new instance unless I'm missing something here. Commented Oct 5, 2018 at 13:45
  • @OHGODSPIDERS the name variable is set in the default constructor, so it shouldn't be possible Edit scratch that, I missed that he set a local variable Commented Oct 5, 2018 at 13:47
  • Are you trying to show the name of c1 or create a new Candidate and show the name of that newly created candidate? Commented Oct 5, 2018 at 13:54
  • @Matthew I am trying to show the name of c1, but it returns null all the time. Commented Oct 5, 2018 at 13:57

4 Answers 4

3

just replace

c1 = showMessage();

with,

showMessage(c1);

and modify method showMessage like the following

private static void showMessage(Candidate c ){    

    System.out.println(c.getName());
}

so similarly, you can use showMessage to print messages for remaining objects like

showMessage(c2);
showMessage(c3);
showMessage(c4);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for helping, putting class and object in method is really new to me, appreciate the guidance.
this changes the functionality, doesn't answer the question. his problem is the local variable in the default constructor, as Davidxxx mentioned
@Stultuske i understood that he is interested on showMessage(), as you can see what do you think showMessage() meant for? he is trying to print messages, i think he is not interested in initializing . he is already doing it that from input.
@mrtammy you are welcome, practice makes perfect. try to experiment what you read.
2

try to pass the candidate inside the showMessage function as a parameter. Then inside the showMessage function use getName() to take the candidates name. Check below :

import java.util.*;

public class Election {
    public static void main(String args[]){
    System.out.println("Welcome to the polls! ");

    Candidate c1 = new Candidate();
    Candidate c2 = new Candidate();
    Candidate c3 = new Candidate();
    Candidate c4 = new Candidate();


    c1 = inputCandidate();
    c2 = inputCandidate();
    c3 = inputCandidate();
    c4 = inputCandidate();



    showMessage(c1);

}

private static Candidate inputCandidate(){
    Scanner sc = new Scanner(System.in);
    String inputN;

    Candidate c = new Candidate();

    System.out.println("Enter candidate");

    inputN = sc.nextLine();
    c.setName(inputN);

    return c;
}

private static void showMessage(Candidate c){


    System.out.println(c.getName());

}


}

Comments

1

Look at the constructor :

public Candidate() {    
  String name = "Not Available! ";
  int votes = 0; 
}

You value a local variable not the name instance field.
These refer two distinct objects. So valuing the one doesn't have any effect on the other.
You committed the same mistake for the votes field but that doesn't cause any issue as an int field has 0 as default value.

So instead, set the name field :

public Candidate() {    
  this.name = "Not Available! ";
  // not required to set votes as it has 0 as default value
}

Or just use field initializers to give a default value to these fields :

public class Candidate {
   //...       
   private String name = "Not Available! ";    
   private int vote; //  0 as default value
   //...
}

2 Comments

Nice catch, missed that one.
@Stultuske Thanks :) But I didn't see right now the dead code for votes
1

try this:

public Candidate() {

 this.name = "Not Available! ";// Previous code is creating a new temp variable by declaring it again, This implementation will set object variable not the temp variable

 this.votes = 0; 

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.