Search code examples
javaeclipsepolymorphism

How to solve error exception java.lang.NoSuchMethodError : 'java.lang.String javax.swing.JOptionPane.showInputDialog(java.lang.String)'


I need to run a program of SaloonMain using eclipse IDE. I imported the library javax.swin.JOptionPane and use the JOptionPane.showInputDialog() to insert data but it popped an error like this :

Exception in thread "main" java.lang.NoSuchMethodError: 'java.lang.String javax.swing.JOptionPane.showInputDialog(java.lang.String)' at lab2.SaloonMain.main(SaloonMain.java:24)

This is my SaloonMain code :

import javax.swing.JOptionPane;

public class SaloonMain {

    public static void main(String[] args) {
        
        String num;
        String name;
        String m;
        String d;
        String r;
        String c;
        String w;
        String dy;
        String t;
        double pay = 0.0;
        
        Saloon[] saloonUser = new Saloon[5];
        
        for(int i = 0; i < saloonUser.length; i++) 
        {
            num = JOptionPane.showInputDialog("Customer's IC number :");
            name = JOptionPane.showInputDialog("Customer's Name :");
            m = JOptionPane.showInputDialog("A member? True | False :");
            boolean m1 = Boolean.parseBoolean(m);
            d = JOptionPane.showInputDialog("Date (MM/DD/YYYY) :");
            
            String choice = JOptionPane.showInputDialog("Choose (1)Hair Treatment | (2)Body Treatment");
            int ans = Integer.parseInt(choice);
            
            boolean r1 = false;
            boolean c1 = false;
            boolean w1 = false;
            boolean dy1 = false;
            
            if(ans==1) 
            {
                r = JOptionPane.showInputDialog("Hair rebonding? (1)Yes | (2)No :");
                if(r=="1") 
                    r1 = true;
                
                c = JOptionPane.showInputDialog("Hair cutting? (1)Yes | (2)No :");
                if(c=="1") 
                    c1 = true;
                
                w = JOptionPane.showInputDialog("Hair washing? (1)Yes | (2)No :");
                if(w=="1") 
                    w1 = true;
                
                dy = JOptionPane.showInputDialog("Hair dyeing? (1)Yes | (2)No :");
                if(dy=="1") 
                    dy1 = true;
                
                HairTreatment ht = new HairTreatment(num,name,m1,d,r1,c1,w1,dy1); 
                pay = pay + ht.computePayment();
            }           
            else 
            {
                
                t = JOptionPane.showInputDialog("Treatment type? (1)Several parts | (2)Full parts :");
                
                if(t=="1") 
                    t = "Several parts";
                else
                    t = "Full parts";
                
                BodyTreatment bt = new BodyTreatment(num,name,m1,d,t);
                pay = pay + bt.computePayment();
            }
            System.out.println("Total : RM" + pay);
        }
    }
}

If anyone could help me on how to solve this, I would be grateful enough. I failed to find any solutions...


Solution

  • It may happen due to wrong configuration, follow these steps:

    1. Right Click on your project folder, Choose Build Path > Configure Build Path.
    2. Select “JRE System Library” and select Edit from the right side bar.
    3. Choose the version of JDK you have installed.
    4. Click on finish and refresh the project.