-3

I am writing a java code by where I have to calculate a summation by using 2 given input value. I create a method in a class Sum. The name of the method is totalVal. Now in the main class I want call it in a switch case statement.I am describing my code in the following way-

At first I have created a enum class with two constant value,

public enum Product {
FIRST(400), SECOND(500);

private int value;

private Product(int value){
    this.value=value;
}
public int getValue() {
    return value;
 }
}

Now in the model class I call this enam class and create another value named num and set getter and setter for this.

public class Model {
  private Product prod;
  private int num;

  public Product getProd() {
     return prod;
  }
  public void setProd(Product prod) {
      this.produkte = produkte;
  }
  public int getNum() {
     return num;
  }
  public void setNume(int num) {
      this.prod = prod;
    }
 }

Now I create another class for the calculation method

public class Sum{
   public int totalVal(Model mod){
     int sum = mod.getNum()*mod.getProd().getValue();
     return sum;
   }
 }

Now in the main method I want to set value of num and implement a switch case statement, so that when user choose First it will give a result with multiply by the num value. on the other hand if user choose SECOND it will give a result with multiply by num value. but I am having problem in switch case method, it shows null poi exception error. I described it more clearly in the code-

public static void main(String[] args) {

    Scanner sc1 =new Scanner(System.in);
    Scanner sc2 =new Scanner(System.in);
    System.out.println("1\tFirst: 400\n2\tSecond: 700 ");
    System.out.println("Please choose your 1 for Compact and 2 for product");
    int swichValue=sc1.nextInt();
    Model md=new Model(); 
    System.out.println("enter number");
    md.setNum(sc2.nextInt()); 
    switch(swichValue){
    case 1:{
        Sum tot=new Sum();
        int res=Product.FIRST.getValue()*tot.Sum(md); //having problem here I am not sure how to call the method here.
        System.out.println("total value is : "+res);
        break;
        }
        case 2:{
        int res=Product.SECOND.getValue()*tot.Sum(md);// problem
        System.out.println("total value is  : "+res);
        break;
        }
   }
}
hina
  • 3
  • 3
  • tot.Sum -> tot.totalVal(mod) and what is "mod" ? shouldn't be "md" there ? – iMysak Apr 20 '17 at 23:06
  • @iMysak I have modified . but still got the error java.lang.NullPointerException – hina Apr 20 '17 at 23:09
  • Show us the stack trace for the NPE. It will tell you what line it occurred on, which helps so much when trying to figure it out... – AHungerArtist Apr 20 '17 at 23:13
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – takendarkk Apr 20 '17 at 23:15
  • `Sum tot=new Sum();` you have a variable named `tot` which is an object of class `Sum`. `tot.Sum(md)` is trying to call a method named `Sum` on the object `tot` -- but the _class_ `Sum` doesn't have a method _named_ `Sum`, it only has a method named `totalVal` ... e.g. `tot.totalVal(md)` – Stephen P Apr 20 '17 at 23:18

1 Answers1

1

look on method totalVal(..)

 int sum = mod.getNum()*mod.getProd().getValue();

In fact I believe you got NPE there. mod should ne not Null and mod.getProd should return Product but not null.

I don't see where you asign product to your md object.

UPD.

you don't need two Scanner's, you can do the same with just one.

UPD.2

I guess instead of

int res=Product.FIRST.getValue()*tot.Sum(md); 

you wanna write

md.setProd(Product.FIRST);
int res = tot.Sum(md); 

and similar in another switch-case.

iMysak
  • 2,170
  • 1
  • 22
  • 35
  • thnk you for your answer. but I am new in java. Could you please inform me what can I do in this case. Shall I set a value to mod.getProd – hina Apr 20 '17 at 23:16
  • I am not sure. is there any writing problem in this line int res=.tot.Sum(md); – hina Apr 20 '17 at 23:21