错误信息“java.lang.NoSuchMethodError: Customer.<init>(Ljava/lang/String;D)V”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22649610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:05:31  来源:igfitidea点击:

Error message "java.lang.NoSuchMethodError: Customer.<init>(Ljava/lang/String;D)V "

javaclassobject

提问by SdlS

I keep getting the same error message when running this program. This is what i get:

运行此程序时,我不断收到相同的错误消息。这就是我得到的:

Exception in thread "main" java.lang.NoSuchMethodError: Customer.<init>(Ljava/lang/String;D)V 
at Customer5.input(Customer5.java:35)<b>---(See below (code) to refer to lines 35 & 7)
at Customer5.main(Customer5.java:7)

Another thing is that the class "Customer" shows a message that says "The type Customer is already defined."

另一件事是类“Customer”显示一条消息,指出“类型 Customer 已经定义”。

import java.util.*;
public class Customer5 {

    public static void main(String[] args) {


        Customer[] customers=input();//This is line 7
        customers=sort(customers);
        output(customers);
    }


    public static Customer[] input(){

        Scanner input =new Scanner(System.in);
        int n;


        String name;
        double debt;


        System.out.print("Enter number of customers :");
        n=input.nextInt();

        Customer[] customers=new Customer[n];
        for(int i=0; i<n; i++){

            System.out.print("Enter name:");
            name=input.next();

            System.out.print("Enter debt:");
            debt=input.nextDouble();

            customers[i]=new Customer(name, debt);//This is line 35
        }
        return customers;
    }


    public static Customer[] sort(Customer[] customers){

        Customer temp;
        for(int i=0; i<customers.length; i++){

             for(int j=i+1; j<customers.length; j++){

                   if(customers[j-1].debt>customers[j].debt){

                         temp=customers[j-1];
                         customers[j-1]=customers[j];
                         customers[j]=temp;
                   }
             }
        }
              return customers;
    }



     public static void output(Customer[] customers){
            for(int i=0; i<customers.length; i++){
             System.out.println(customers[i].name+"\t" +customers[i].debt);
            }
     }



} 

class Customer{ //this line shows a message that says:The type Customer is already defined

    public String name;
    public Double debt;

    public Customer(String name, double debt){
       this.name=name;
       this.debt=debt;
    }

}

I don't know what to do in order to fix it. I am not very familiar with this type of error message. I'd really appreciate any feedback or comments on how to approach this problem. Thanks!

我不知道该怎么做才能修复它。我对这种类型的错误消息不是很熟悉。我非常感谢有关如何解决此问题的任何反馈或评论。谢谢!

采纳答案by libik

Tested, it works fine for me.

经测试,对我来说效果很好。

Find "clear and build" option in your IDE, it should do the trick.

在您的 IDE 中找到“清除并构建”选项,它应该可以解决问题。



Also make sure, that in your project does not exist other Customer class.

还要确保在您的项目中不存在其他 Customer 类。

回答by Javaboy

This Class runs fine for me, issue is there must be some other Customer class in your package or application which doesnt have constructor public Customer(String name, double debt).

这个类对我来说运行良好,问题是你的包或应用程序中必须有一些其他的 Customer 类没有构造函数public Customer(String name, double debt)

Try clicking Ctrl + Shift + R and search Customer or Press Ctrl + H, search Customer in all java files

尝试单击 Ctrl + Shift + R 并搜索 Customer 或按 Ctrl + H,在所有 java 文件中搜索 Customer