עזרה יש לי שאלה בג'אווה

nhfk

משתמש סופר מקצוען
הנדסת תוכנה
עיצוב ואדריכלות פנים
הוא עושה לי שגיאת ריצה על קטע הקוד הבא. (מה שמוקף באדום)
מישו מזהה אותה?
הוא כותב לי:
NullPointerException
upload_2019-1-4_2-55-28.png
 

nhfk

משתמש סופר מקצוען
הנדסת תוכנה
עיצוב ואדריכלות פנים
מה שמשונה זה שיש לי שיטה נוספת בתוכנית שיש שם שורה אותו דבר ושם זה לא עושה שגיאת ריצה:
upload_2019-1-4_3-7-2.png
 

אמא ומתכנתת

משתמש סופר מקצוען
מנוי פרימיום
כתיבה ספרותית
אם הוא באמת נופל על השורה שסימנת, לכאורה הבעיה היא ש_cities לא מאותחל נכון.
(בפעם השניה הוא כנראה כבר מאותחל)
 

ביגר

משתמש סופר מקצוען
הנדסת תוכנה
D I G I T A L
או שהמקום i שהוא נופל עליו בcities לא מאותחל
 

nhfk

משתמש סופר מקצוען
הנדסת תוכנה
עיצוב ואדריכלות פנים
אין מצב. זה השיטה האחרונה ויש לפני זה מלא שיטות עם אותם נתונים וזה רץ יופי.
ניסיתי לשנות בהגדרה של הלולאה את התנאי ל: _noOfCities - 1 וזה באמת לא עשה לי שגיאת ריצה אבל אז בפלט חסר המקום האחרון.
 

nhfk

משתמש סופר מקצוען
הנדסת תוכנה
עיצוב ואדריכלות פנים
זה התוכנית השלמה:


public class Country
{
private String _countryName;
private int _noOfCities;
private City[] _cities;
private final int MAX_NUM_CITIES = 1000;

public Country(String name)
{
// initialise instance variables
_countryName = name;
_cities = new City[MAX_NUM_CITIES];
_noOfCities = 0;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public boolean addCity(String cityName, double xCityCenter, double yCityCenter, double xCentralStation, double yCentralStation, int numOfResidents, int numOfNeighborhood)
{
// put your code here
if(_noOfCities < MAX_NUM_CITIES)
{
_cities[_noOfCities] = new City(cityName, xCityCenter, yCityCenter, xCentralStation, yCentralStation, numOfResidents, numOfNeighborhood);
_noOfCities++;
return true;
}
return false;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int getNumOfResidents()
{
// put your code here
int count = 0;
for(int i = 0;i<_noOfCities;i++)
{
count += _cities.getNumOfResidents();
}
return count;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
private double distanceCenters(City a, City b)
{
// put your code here
Point centerA = a.getCityCenter();
Point centerB = b.getCityCenter();
double distance = centerA.distance(centerB);
return distance;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public double longestDistance()
{
// put your code here
if (_noOfCities<2)
{
return 0;
}
else
{
boolean found = true;
double longest = 0;
for(int i = 0;i<_noOfCities;i++)
{
for(int j =1;j<_noOfCities;j++)
{
double distance =distanceCenters(_cities , _cities[j]);
if(distance>longest)
{
longest = distance;
}
}
}
return longest;
}
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
private City findCity(String cityName)
{
// put your code here
for (int i = 0;i<_noOfCities;i++)
{
String name = _cities.getCityName();
if(name.equals(cityName))
{
return _cities;
}
}
return null;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public String citiesNorthOf(String nameCity)
{
// put your code here
City city = findCity(nameCity);
Point center = city.getCityCenter();
String x = "The cities north of" +nameCity+ "are: "+"\n";
String res ="The cities north of " +nameCity+ " are: "+"\n";
if(city.equals(null))
{
return "There is no city with the name "+ nameCity;
}
for(int i = 0;i<_noOfCities;i++)
{
if (_cities.getCityCenter().isAbove(center))
{
res = res + _cities.toString()+"\n"+"\n";
}
}
if(res.equals(x))
{
return "There are no cities north of " + nameCity;
}
else
{
return res;
}
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public City southernmostCity()
{
// put your code here
City min = _cities[0];
for(int i = 1;i<_noOfCities;i++)
{
Point center = _cities.getCityCenter();
if (center.isUnder(min.getCityCenter()))
{
min = _cities;
}
}
if(_noOfCities == 0)
{
return null;
}
else
{
return min;
}
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public String getCountryName()
{
// put your code here
return _countryName;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int getNumOfCities()
{
// put your code here
return _noOfCities;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public City[] getCities()
{
// put your code here
City[] copy = new City[_noOfCities];
int i, j;
for(i = 0, j=0;i<_noOfCities;i++,j++)
{
copy = _cities[j];
}
return copy;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public City unifyCities(String city1, String city2)
{
// put your code here
City a = findCity(city1);
City b = findCity(city2);
long noOfResidents = a.getNumOfResidents() + b.getNumOfResidents();
int noOfNeighborhoods = a.getNoOfNeighborhoods() +b.getNoOfNeighborhoods();
Point centerA = a.getCityCenter();
double xCenterA = centerA.getX();
double yCenterA = centerA.getY();
Point centerB = b.getCityCenter();
double xCenterB = centerB.getX();
double yCenterB = centerB.getY();
double xCenterNew = (xCenterA + xCenterB)/2;
double yCenterNew = (yCenterA + yCenterB)/2;
Point stationA = a.getCentralStation();
double xStationA = stationA.getX();
double ystationA = stationA.getY();
Point stationB = b.getCentralStation();
double xStationB = stationB.getX();
double yStationB = stationB.getY();
double xStationNew;
double yStationNew;
if (stationA.isLeft(stationB))
{
xStationNew = xStationA;
yStationNew = ystationA;
}
else
{
xStationNew = xStationB;
yStationNew = yStationB;
}
City unifyCities = new City("city2-city1", xCenterNew, yCenterNew, xStationNew, yStationNew, noOfResidents, noOfNeighborhoods);
String AcityName = a.getCityName();
int indexA =0;
for(int i = 0;i<_noOfCities;i++)
{
String name = _cities.getCityName();
if(name.equals(AcityName))
{
indexA = i;
}
}
String BcityName = b.getCityName();
int indexB=0;
for(int i = 0;i<_noOfCities;i++)
{
String name = _cities.getCityName();
if(name.equals(BcityName))
{
indexB = i;
}
}
if(a.getNumOfResidents()<b.getNumOfResidents())
{
_cities[indexB] = new City(unifyCities);
for(int i=indexA;i<_noOfCities-1;i++)
{
_cities = _cities[i+1];
}
}
else
{
_cities[indexA] = new City(unifyCities);
for(int i=indexB;i<_noOfCities-1;i++)
{
_cities = _cities[i+1];
}
}
_cities[_noOfCities-1]=null;
return unifyCities;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public String toString()
{
// put your code here
String res = "Cities of "+getCountryName()+":" +"\n";
String x = "Cities of "+getCountryName()+":" +"\n";
for(int i = 0;i<_noOfCities;i++)
{
res = res + _cities.toString()+"\n"+"\n";
}
if(res.equals(x))
{
return "There are no cities in this country." ;
}
else
{
return res;
}
}
}
 

nhfk

משתמש סופר מקצוען
הנדסת תוכנה
עיצוב ואדריכלות פנים
טוב, זה באמת ארוך...
בכל אופן מישהו??!
אני צריכה להגיש כבר ואני ממש תקועה על זה.
לא ברור מה הסיפור.
 

אמא ומתכנתת

משתמש סופר מקצוען
מנוי פרימיום
כתיבה ספרותית
אין מצב. זה השיטה האחרונה ויש לפני זה מלא שיטות עם אותם נתונים וזה רץ יופי.
ניסיתי לשנות בהגדרה של הלולאה את התנאי ל: _noOfCities - 1 וזה באמת לא עשה לי שגיאת ריצה אבל אז בפלט חסר המקום האחרון.
קצת קשה לקרוא את הקוד ככה, כשהכל מיושר לימין...
אבל בכל מקרה, אם את אומרת שהשינוי הזה עזר - כנראה שהבעיה היא באיבר האחרון במערך.
יש לך אפשרות לדבג? זה יהיה הכי יעיל במקרה הזה, לעצור בשורה הבעיתית ולבדוק מה באמת יש בכל משתנה.
אם לא, תנסי להוסיף הדפסה של המערך לפני הלולאה הבעיתית, כך שזה ישקף לך מה יש בו.
 

nhfk

משתמש סופר מקצוען
הנדסת תוכנה
עיצוב ואדריכלות פנים
קצת קשה לקרוא את הקוד ככה, כשהכל מיושר לימין...
אבל בכל מקרה, אם את אומרת שהשינוי הזה עזר - כנראה שהבעיה היא באיבר האחרון במערך.
יש לך אפשרות לדבג? זה יהיה הכי יעיל במקרה הזה, לעצור בשורה הבעיתית ולבדוק מה באמת יש בכל משתנה.
אם לא, תנסי להוסיף הדפסה של המערך לפני הלולאה הבעיתית, כך שזה ישקף לך מה יש בו.
תכלס זה מה שעשיתי וזה הדפיס לי את המערך כולו בלי המיקום האחרון.
הבעיה היא שאני לא מוצאת שום פגם במקום האחרון, הוא אותחל בדיוק כמו קודמיו.
יש מצב שאת מסבירה לי איך מדבגים (ככה כותבים...?) או שזה מסובך?
 

nhfk

משתמש סופר מקצוען
הנדסת תוכנה
עיצוב ואדריכלות פנים
זה הבנאי:
upload_2019-1-6_1-11-25.png

זה השיטה שמוסיפה ערים למערך והיא השיטה שהשתמשתי בה ראשונה במיין אחרי שהפעלתי את הבנאי:
upload_2019-1-6_1-9-40.png

ואז רציתי לעשות את השיטה מדוברת לעיל.
מבינה?
@אמא ומתכנתת
 

nhfk

משתמש סופר מקצוען
הנדסת תוכנה
עיצוב ואדריכלות פנים
ממש תודה רבה בכל מקרה!!
 

P.S.

משתמש מקצוען
השיטה שמוסיפה ערים, אחרי כל הוספה, ה-_no Of Cities גדל באחד לפני התנאי שבודק האם להמשיך בלולאה.
ככה שיוצא שבפעם האחרונה הוא גדל ולא נכנס שוב ללולאה.
לכן המקום האחרון לא מאותחל
 

nhfk

משתמש סופר מקצוען
הנדסת תוכנה
עיצוב ואדריכלות פנים
השיטה שמוסיפה ערים, אחרי כל הוספה, ה-_no Of Cities גדל באחד לפני התנאי שבודק האם להמשיך בלולאה.
ככה שיוצא שבפעם האחרונה הוא גדל ולא נכנס שוב ללולאה.
לכן המקום האחרון לא מאותחל
לא מאוד הבנתי.
זה לא לולאה זה IF, והוא נכנס בפעם האחרונה מעדכן את CITIES_ ואז מגדיל את NO OF CITIES, לא?
 

P.S.

משתמש מקצוען
צודקת, לא קראתי את הקוד נכון
אבל עדיין ההסבר הוא אותו הסבר
כשאת נכנסת לפונקציה שמוסיפה עיר, קודם יש הוספה למיקום האחרון, ואח"כ את מגדילה את ה _noOfCities באחד
ז"א שאחרי שאת יוצאת מהפונקציה, תמיד המספר גדול באחד, מאשר התאים המותחלים
 

יוכבדא

משתמש סופר מקצוען
עיצוב גרפי
הנדסת תוכנה
צודקת, לא קראתי את הקוד נכון
אבל עדיין ההסבר הוא אותו הסבר
כשאת נכנסת לפונקציה שמוסיפה עיר, קודם יש הוספה למיקום האחרון, ואח"כ את מגדילה את ה _noOfCities באחד
ז"א שאחרי שאת יוצאת מהפונקציה, תמיד המספר גדול באחד, מאשר התאים המותחלים
אבל לכן אח"כ היא בודקת
_noOfCities>
ולא >=
_noOfCities
 

יוכבדא

משתמש סופר מקצוען
עיצוב גרפי
הנדסת תוכנה

nhfk

משתמש סופר מקצוען
הנדסת תוכנה
עיצוב ואדריכלות פנים
אז ככה:
השיטה שמוסיפה ערים, אחרי כל הוספה, ה-_no Of Cities גדל באחד לפני התנאי שבודק האם להמשיך בלולאה.
ככה שיוצא שבפעם האחרונה הוא גדל ולא נכנס שוב ללולאה.
לכן המקום האחרון לא מאותחל
שיניתי את זה ל -
upload_2019-1-6_16-39-40.png

זה לא עזר.
אני כלל לא מבין למה הלולאה בכלל
מקריאת הקוד אפשר למחוק את כל הלולאה
הדרישות ממני היו:
upload_2019-1-6_16-41-57.png

למה לא צריך לולאה?
הקוד שצרפת שונה מהקוד שצילמת...​
for(int i = 0;i<_noOfCities;i++)
{
res = res + _cities.toString()+"\n"+"\n";
}​
יכול להיות... כי כל הזמן שיניתי כדי לבדוק איפה הטעות...

אני לא יודעת באיזה עורך את משתמשת ואיך את מריצה, יש עורכים שיש בהם פונקציות מובנות לדיבאג.
מצאתי איך עושים את זה - אבל זה לא מאוד עזר לי. הגעתי למה שאתם אמרתם לי שהמקום האחרון לא מאותחל.
אפשר לראות איך בדיוק השתמשת בה?
זה הטסטר:
upload_2019-1-6_16-44-12.png


בכל מקרה יישר כח גדול לכולם!!
לוידת מה הייתי עושה.
 

אולי מעניין אותך גם...

לוח מודעות

למעלה