#Problem with an array
58 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @torn niche! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
class Test {
private static final String DATA_FILE = "First_Nation_Infrastructure_Investment.csv";
private static final String LOG_FILE = "log.txt";
public static void main(String[] args) {
Project[] projects = loadDataFromFile(DATA_FILE);
displayMainMenu(projects);
}
public static void logPrint(String pPrompt)
{
System.out.println(pPrompt);
writeLog(pPrompt);
}
private static Project[] loadDataFromFile(String filename) {
List<Project> projectList = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line = reader.readLine(); // Skip header line
while ((line = reader.readLine()) != null) {
String[] values = line.split(",");
String province = values[0];
String beneficiary = values[1];
String beneficiaryNum = values[2];
String assetClass = values[3];
String name = values[4];
String stage = values[6];
double latitude;
try {
latitude = Double.parseDouble(values[7]);
} catch (NumberFormatException e) {
logPrint("Invalid latitude value for project: " + name);
continue; // Skip this line and proceed to the next iteration
}
double longitude;
try {
longitude = Double.parseDouble(values[8]);
} catch (NumberFormatException e) {
logPrint("Invalid longitude value for project: " + name);
continue; // Skip this line and proceed to the next iteration
}
String coordinateSystem = values[9];
Location location = new Location(latitude, longitude, coordinateSystem);
Project project = new Project(province, beneficiary, beneficiaryNum, assetClass, name, stage, location);
projectList.add(project);
}
}catch (IOException e) {
logPrint("Error reading data file: " + e.getMessage());
}
return projectList.toArray(new Project[0]);
}
What do you mean, "all the data from the csv that is read into the array is null"?
What do you see when you do what where?
I can force print the array 'projects' and it outputs null for everything
Example of menu
15 works
2-14 will bring up a submenu
In the submenu case 6 works
but in main menu selecting 1 (All of Canda) will give this error
same thing for selecting 1-5 in the submenu
the csv is fine because it works in another project
I have a feeling its a problem with trying to call from other methods but I can't figure out how to fix it
@silk tundra Would the .java files help at all?
example of force printed array
line #254 is causing NPE whats on it?
?
see the stacktrace in this image
the Exception in... thats a stacktrace
it lets the developer know what went wrong
and where
so you want to see 254?
Very important to ask. You don't want to show that line in case that wasn't the question, think of what could happen
Wdym?
I mean show the code at the lines asked. Don't ask questions, what's the point?
alright here is an updated view
You're wasting time, for what? This is extremely frustrating.
So, I've tried your code because with my eyes I had the impression that loadDataFromFile() cannot produce what you're showing us.
That's how I saw that loadDataFromFile() cannot even compile. So You're not running the code you're showing us.
Bleh. Copy-y-paste shenanigans, I've not proven it yet actually. Keeping at it
I guess you'd need to show class Project. The constructor/variables are probably messed up
And class Project is so huge that you can't show it without forcing everyone to deal with a zip?
import java.util.IllegalFormatException;
public class Project
{
private String province;
private String beneficiary;
private String beneficiaryNum;
private String assetClass;
private String name;
private String stage;
private Location theLocation;
//Constructor with parameters
public Project(String pProvince, String pBeneficiary, String pBeneficiaryNum, String pAssetClass, String pName, String pStage, Location pLocation)
{
pProvince = province;
pBeneficiary = beneficiary;
pBeneficiaryNum = beneficiaryNum;
pAssetClass = assetClass;
pName = name;
pStage = stage;
pLocation = theLocation;
}
//Copy Constructor
public Project(Project project1)
{
province = project1.getProvince();
beneficiary = project1.getBeneficiary();
beneficiaryNum = project1.getBeneficiaryNum();
assetClass = project1.getAssetClass();
name = project1.getName();
stage = project1.getStage();
theLocation = new Location(project1.getLocation());
}
//Default Constructor
public Project()
{
province = "Quebec";
beneficiary = "First Nations of Quebec and Labrador Sustaianable Development institute";
beneficiaryNum = "9999";
assetClass = "Energy, Sustainability and connectivity";
name = "Comprehensive Community Planning";
stage = "Completed";
theLocation = new Location();
}
//Accessors
public String getProvince() {
return province;
}
public String getBeneficiary() {
return beneficiary;
}
public String getBeneficiaryNum() {
return beneficiaryNum;
}
public String getAssetClass() {
return assetClass;
}
public String getName() {
return name;
}
public String getStage() {
return stage;
}
public Location getLocation() {
return theLocation;
}
//Mutators
public void setProvince(String pProvince)
{
if(pProvince == "Alberta" || pProvince == "British Columbia" || pProvince == "Manitoba" || pProvince == "New Brunswick" || pProvince == "newfoundland And Labrador" || pProvince == "Nove Scotia" || pProvince == "Ontario" || pProvince == "Prince Edward Island" || pProvince == "Quebec" || pProvince == "Saskatchewan" || pProvince == "Northwest Territories" || pProvince == "Nunavut" || pProvince == "Yukon")
{
province = pProvince;
}
else
{
throw new IllegalArgumentException();
}
}
public void setBeneficiary(String pBeneficiary)
{
if(pBeneficiary != null)
{
beneficiary = pBeneficiary;
}
else
{
throw new IllegalArgumentException();
}
}
public void setBeneficiaryNum(String pBeneficiaryNum)
{
if(pBeneficiaryNum != null)
{
beneficiary = pBeneficiaryNum;
}
else
{
throw new IllegalArgumentException();
}
}
public void setAssetClass(String pAssetClass)
{
if(pAssetClass != null)
{
assetClass = pAssetClass;
}
else
{
throw new IllegalArgumentException();
}
}
public void setName(String pName)
{
if(pName != null)
{
name = pName;
}
else
{
throw new IllegalArgumentException();
}
}
public void setStage(String pStage)
{
if(pStage == "Ongoing" || pStage == "Completed")
{
stage = pStage;
}
else
{
throw new IllegalArgumentException();
}
}
public void setLocation(Location pLocation)
{
if(pLocation != null)
{
theLocation = pLocation;
}
else
{
throw new IllegalArgumentException();
}
}
public String toString()
{
String projectString;
projectString = "Province: " + province
+ "\nBeneficiary: " + beneficiary
+ "\nBeneficiary Number: " + beneficiaryNum
+ "\nAsset Class: " + assetClass
+ "\nName: " + name
+ "\nStage: " + stage
+ "\n" + theLocation;
return projectString;
}
public boolean equals(Object inObject)
{
boolean isEqual = false;
Project inProject = null;
if(inObject instanceof Project)
{
inProject = (Project)inObject;
if(province.equals(inProject.getProvince()))
if(beneficiary.equals(inProject.getBeneficiary()))
if(beneficiaryNum.equals(inProject.getBeneficiaryNum()))
if(assetClass.equals(inProject.getAssetClass()))
if(name.equals(inProject.getName()))
if(stage.equals(inProject.getStage()))
if(theLocation == inProject.getLocation())
isEqual = true;
}
return isEqual;
}
}
thats class Project
Excellent. Now check the constructor, you've inverted the names
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.