簡體   English   中英

Java Runtime Exception的主要目標,發生原因以及逃避Java Runtime異常的最佳實踐

[英]Main objectives of java Runtime Exception and why they occur as well as the best practices for evasion of java runtime exception

各位務實的程序員,您好,我開發了這個程序,它是一個用Java編寫的社交網絡程序,該程序的主要功能是允許用戶:添加朋友,刪除朋友,查看誰擁有最多的朋友,查看具有最高影響力並有能力退出該計划的人。

就像任何程序一樣,都會出現一兩個斑點,對我來說,這是Java運行時異常的一種情況,在我嘗試通過不使用主程序包就可以解決問題或嘗試將其改正后,這個問題就一直存在。更改我編寫程序的方式無濟於事。

這是不斷出現的錯誤消息:

Select:
[1] Add Friend
[2] Delete Friend
[3] List Friends
[4] Friends of Friends
[5] Most Popular
[6] Most Influencer
[7] Exit
7
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: uelbook.Person
    at uelbook.UELbook.main(UELbook.java:20)

我的主要課程的第20行是UELbook,下面是:

 Person listfriends=new Person();

UELbook:

package uelbook;
import java.util.*;
import java.io.*;

public class UELbook {

    public static void main(String[] args) {
        UELbook uelbook = new UELbook();
        //test your code here
                Scanner scanner=new Scanner(System.in);
                System.out.println("Select:");
                System.out.println("[1] Add Friend");
                System.out.println("[2] Delete Friend");
                System.out.println("[3] List Friends");
                System.out.println("[4] Friends of Friends");
                System.out.println("[5] Most Popular");
                System.out.println("[6] Most Influencer");
                System.out.println("[7] Exit");
               int choice=scanner.nextInt();
               Person listfriends=new Person();
                while(choice!=7){
                 int num=0;   

                //ADD
                if(choice==1){
                System.out.println("ID: ");
                String id=scanner.nextLine();
                System.out.println("Username: ");
                String username=scanner.nextLine();
                System.out.println("Password: ");
                String password=scanner.nextLine();
                listfriends.set(id, username, password);
                listfriends.addFriend();
                }
                //DELETE
                if(choice==2){
                System.out.println("ID: ");
                String id=scanner.nextLine();
                System.out.println("Username: ");
                String username=scanner.nextLine();
                System.out.println("Password: ");
                String password=scanner.nextLine();
                listfriends.set(id, username, password);
                listfriends.removeFriend();
                }
                //LIST
                if(choice==3){
                    for(int i=0;i<1;i++){
                    System.out.println(listfriends.list);
                    }
                }

                System.out.println("ID: ");
                String id=scanner.nextLine();
                System.out.println("Username: ");
                String username=scanner.nextLine();
                System.out.println("Password: ");
                String password=scanner.nextLine();

                //REMOVE
                if(choice==7){
                System.exit(0);  
                }

                if(choice==8){
                    System.out.println("ERROR! Please choose from the options.");
                }

                }//WHILE LOOP EXIT
    }
    public void addPerson(String id, String firstname, String lastname) throws PersonExistsException {
        //list.add(ID);
               // list.add(firstname);
                //list.add(lastname);
    }
    public String getPerson(String id) throws NoSuchCodeException {
        return id; 
    }
    public void addFriendship(String id1, String id2) throws NoSuchCodeException {
            id1=id1;
            id2=id2;
    }
    public Collection<String> listFriends(String id) throws NoSuchCodeException {
        return null; 
    }
    public Collection<String> friendsOfFriends(String id) throws NoSuchCodeException {
        return null;
    }
    //The methods returns true if the file has been loaded, 
    //false in case of any errors
    public boolean loadFile(String file) {
        return false; // remove this in the implementation
    }
    //The methods returns true if the file has been saved, 
    //false in case of any errors
    public boolean saveFile(String file) {
        return false; // remove this in the implementation
    }
    public String mostPopular() {
        return null; // remove this in the implementation
    }
    public String mostInfluencer() {
        return null; // remove this in the implementation
    }
}

人:

package UELbook;

import java.util.*;
import java.io.*;

public class Person {

    String ID;
    String firstname;
    String lastname;
    ArrayList<String>list=new ArrayList<String>();

    public static void main(String[]args){

    }

    //CONSTRUCTOR
    public void set(String ID, String firstname, String lastname){
        setID(ID);
        setFirstName(firstname);
        setLastName(lastname);
    }

    //SETTERS
    public void setID(String ID){
        ID=ID;
    }

    public void setFirstName(String FirstName){
       firstname=FirstName;
    }

    public void setLastName(String LastName){
        lastname=LastName;
    }

    //GETTERS
    public String getID(){
        return ID;
    }

    public String getFirstName(){
        return firstname;
    }

    public String getLastName(){
        return lastname;
    }

     public void addPerson(String id, String firstname, String lastname) throws uelbook.PersonExistsException {
        list.add(ID);
                list.add(firstname);
                list.add(lastname);
    }   

}

人員存在例外:

package uelbook;

@SuppressWarnings("serial")
public class PersonExistsException extends Exception {

}

沒有這樣的代碼異常:

package uelbook;

@SuppressWarnings("serial")
public class NoSuchCodeException extends Exception {

}

任何有用的提示將不勝感激。

您的Person類正在使用包UELbook

大概應該是uelbook


(已更新以提供解釋。。。)

只是慣例(盡管遵循99.99%的時間),程序包名稱應全部小寫。 Oracle指出以下原因:

包名稱以小寫形式編寫, 以避免與類或接口的名稱沖突

在OP中的代碼中有:

  • 名為UELbook的軟件包
  • 另一個名為uelbook的軟件包
  • 名為UELbook的類

因此,您將在類UELbook和包UELbook之間發生沖突。 從技術角度來看,將軟件包名稱UELbook更改為uelbook應該可以解決您的問題。 但是,作為一個相關問題,此后,您應該更改軟件包uelbook或類UELbook的名稱,以消除所有歧義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM