实例化和初始化对象时出现问题

时间:2018-10-13 10:19:09

标签: java objectinstantiation

我一直在编码我的一个最终学校项目中的一个,该项目与基于婚礼的文本文件有关。 我正在尝试编写一个将返回婚礼对象的方法,

(注意:除草对象由新娘名称,新郎名称,婚礼日期,地点,来宾人数组成)。

在使用字符串的常规方法中。例如,我只输入

String temp = ""; 

运行一个循环以遍历我的数组。 if语句

temp = temp + arr[loop].toString();

return temp;

但是现在当我声明一个婚礼对象时:

Wedding temp; - (As i cant initialize it as there is no brideName etc.)
run loop
if statement
temp = temp + array[loop];

return temp; 

在这里,我得到的温度错误可能尚未初始化。

有人可以提出解决建议吗?非常感谢 这是实际方法的样子

 public Wedding getWeddingsOnDay(String date, String venue)
{
    Wedding temp;
    for (int loop = 0; loop < counter; loop++)
    {
        if (wedArr[loop].getWeddingDate().equals(date) && wedArr[loop].getVenue().equals(venue))
            temp = wedArr[loop];
        else
            temp = null;
    }
    return temp;
}

3 个答案:

答案 0 :(得分:1)

以这种方式重写方法。

public Wedding getWeddingAt(String date, String venue) {
    for (Wedding w : wedArr)
    {
        if (w.getWeddingDate().equals(date) &&
                w.getVenue().equals(venue)) {
            return w;
        }
    }
    return null;
}

答案 1 :(得分:0)

全面解决您的问题:

package pojo.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Wedding {

    private String groomName;
    private String brideName;
    private String weddingDate;
    private String venue;
    private int numberOfGuests;
    /**
     * 
     */
    public Wedding() {
        super();
    }
    /**
     * @return the groomName
     */
    public String getGroomName() {
        return groomName;
    }
    /**
     * @param groomName the groomName to set
     */
    public void setGroomName(String groomName) {
        this.groomName = groomName;
    }
    /**
     * @return the brideName
     */
    public String getBrideName() {
        return brideName;
    }
    /**
     * @param brideName the brideName to set
     */
    public void setBrideName(String brideName) {
        this.brideName = brideName;
    }
    /**
     * @return the weddingDate
     */
    public String getWeddingDate() {
        return weddingDate;
    }
    /**
     * @param weddingDate the weddingDate to set
     */
    public void setWeddingDate(String weddingDate) {
        this.weddingDate = weddingDate;
    }
    /**
     * @return the venue
     */
    public String getVenue() {
        return venue;
    }
    /**
     * @param venue the venue to set
     */
    public void setVenue(String venue) {
        this.venue = venue;
    }
    /**
     * @return the numberOfGuests
     */
    public int getNumberOfGuests() {
        return numberOfGuests;
    }
    /**
     * @param numberOfGuests the numberOfGuests to set
     */
    public void setNumberOfGuests(int numberOfGuests) {
        this.numberOfGuests = numberOfGuests;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "[groomName=" + groomName + ", brideName=" + brideName + ", weddingDate=" + weddingDate
                + ", venue=" + venue + ", numberOfGuests=" + numberOfGuests + "]\n";
    }

    /**
     * 
     * @param weddingList
     * @param date
     * @param venue
     * @return
     */
    public List<Wedding> getWeddingsOnDay(List<Wedding> weddingList, String date, String venue)
    {
        List<Wedding> weddingFound = new ArrayList<Wedding>(); 
        for (Wedding wed : weddingList)
        {
            if (wed.getWeddingDate().equals(date) && wed.getVenue().equals(venue))
                weddingFound.add(wed);
        }
        return weddingFound;
    }

}

/**
 * Testing 
 * 
 * @author Anish
 *
 */
public class Test {
     public static void main(String...args) {

         List<Wedding> weddingList = new ArrayList<Wedding>();

         //first data
         Wedding weddingFirstCouple = new Wedding();
         weddingFirstCouple.setBrideName("Elisabeth");
         weddingFirstCouple.setGroomName("Michael");
         weddingFirstCouple.setWeddingDate("29 October 2018");
         weddingFirstCouple.setVenue("New Country Wedding Hall, Australia");
         weddingFirstCouple.setNumberOfGuests(2000);
         weddingList.add(weddingFirstCouple);

         //second data
         Wedding weddingSecondCouple = new Wedding();
         weddingSecondCouple.setBrideName("Lisa");
         weddingSecondCouple.setGroomName("John");
         weddingSecondCouple.setWeddingDate("15 December 2018");
         weddingSecondCouple.setVenue("Lifelong Wedding Hall, USA");
         weddingSecondCouple.setNumberOfGuests(1500);
         weddingList.add(weddingSecondCouple);

         System.out.println(weddingList);

         //checking the Wedding Date : getWeddingOnDay
         //lets see..
         Wedding wedObj = new Wedding();
         Scanner sc = new Scanner(System.in);
         String weddingDate = sc.nextLine();
         String venue = sc.nextLine();
         List<Wedding> weddingCoupleListOnTheDay = wedObj.getWeddingsOnDay(weddingList, weddingDate, venue);
         for(Wedding weddingCoupleOnTheDay : weddingCoupleListOnTheDay) {
             System.out.println(weddingCoupleOnTheDay);
         }

     }
}

输出-

[[groomName=Michael, brideName=Elisabeth, weddingDate=29 October 2018, venue=New Country Wedding Hall, Australia, numberOfGuests=2000]
, [groomName=John, brideName=Lisa, weddingDate=15 December 2018, venue=Lifelong Wedding Hall, USA, numberOfGuests=1500]
]
29 October 2018  
New Country Wedding Hall, Australia
[groomName=Michael, brideName=Elisabeth, weddingDate=29 October 2018, venue=New Country Wedding Hall, Australia, numberOfGuests=2000]

我建议您将Java集合用于复杂的Java程序,并为每个循环使用 for-each循环

感谢:)

答案 2 :(得分:0)

您的代码将说这显然是因为,您的代码可能不会进入if块,而temp varialble仍未使用。因此,使用它的任何人都需要在使用此temp变量之前检查null值。

有几种方法可以消除此错误:

  1. 忽略此错误。由于此错误不会停止运行您的代码。

  2. 您可以使用可选的intead来重设null,之后,您的方法将像下面的方法一样出现:

    public Optional<Wedding> getWeddingsOnDay(String date, String venue){
    
      Optional<Wedding> temp;
      for (int loop = 0; loop < counter; loop++) {
          if (wedArr[loop].getWeddingDate().equals(date) && wedArr[loop].getVenue().equals(venue)) { 
              temp = Optional.of(wedArr[loop]);}
     else{
              temp = Optional.empty(); 
      }
    }
      return temp;    
    }
    
  3. 您根本不需要使用temp,只需返回 wedArr [loop]或null