当前位置: 代码迷 >> java >> 错误:找不到或加载主类bankAccount.BankAccount
  详细解决方案

错误:找不到或加载主类bankAccount.BankAccount

热度:34   发布时间:2023-07-31 12:06:37.0

我是Java的初学者,试图编译并运行一个简单的程序。 它编译成功。 但是当我尝试运行时抛出错误。 谁能指出我的错误。 错误如下。

"Error: Could not find or load main class bankAccount.BankAccount"

下面是代码。

package bankAccount;

class AccountDetails{
    private String fName;
    private String mName;
    private String lName;
    private String mailId;
    private String dob;
    private char gender;
    private String mStatus;
    private int custID;
    private int accountNo;
    private int minBalance;
    private static int counter=1000;
    private static int counter1=10000;
    private String typeOfAccount;
    public AccountDetails(String fName, String mName, String lName, String mailId, String dob, char gender, String mStatus)
    {
        this.fName=fName;
        this.mName=mName;
        this.lName=lName;
        this.mailId=mailId;
        this.dob=dob;
        this.gender=gender;
        this.mStatus=mStatus;
        custID=++counter;
        accountNo=++counter1;
        if (typeOfAccount.equalsIgnoreCase("salary")){
            minBalance=0;
        }
        else if (typeOfAccount.equalsIgnoreCase("nonsalary")){
        minBalance=1000;
        }
    }
    public void displayAcc(){
        System.out.println("fname: "+fName);
        System.out.println("mname: "+mName);
        System.out.println("lname: "+lName);
        System.out.println("mailId: "+mailId);
        System.out.println("Date of Birth: "+dob);
        System.out.println("gender: "+gender);
        System.out.println("Marital Status: "+mStatus);
        System.out.println("Account No: "+accountNo);
        System.out.println("Customer ID: "+custID);
        System.out.println("Type of Account: "+typeOfAccount);
        System.out.println("Min. Balance: "+minBalance);
        }

}

public class BankAccount {
public static void main(){
    AccountDetails acObj1 = new AccountDetails("Paul","J","AnderSon","Paul@gmail.com","3rd Jan, 1985",'M',"Single");
    acObj1.displayAcc();
}
}

将主要方法的特征从

public static void main(){

public static void main(String[] args){

当运行类BankAccount (或任何其他类)时,jvm搜索以String数组作为参数作为入口点的主要方法,因此在您的情况下,jvm无法找到它,因为您没有正确的参数

对于第二部分

您只声明了typeOfAccount ,但未初始化(如我所见),并且您正在执行typeOfAccount.equalsIgnoreCase()意味着它类似于null.equalsIgnoreCase() (字符串的空默认值),为您提供了空指针异常。

您尚未设置typeOfAccount,因此在此处的构造函数中引用它时;

if (typeOfAccount.equalsIgnoreCase("salary"))

和这里;

else if (typeOfAccount.equalsIgnoreCase("nonsalary"))

typeOfAccount都不等于,因此不会设置minBalance,因此您将在此处获得NullPointerException;

System.out.println("Type of Account: "+typeOfAccount);

和这里;

System.out.println("Min. Balance: "+minBalance);

因为尚未设置这些值。