why am I getting NullPointerException.
guys, why am I getting NullPointerException when initializing the array? I ran debug mode and productName is null
Code :
public class Main {
public static void main(String[] args) {
Product[] myProducts = new Product[3];
Supplier ikea = new Supplier();
ikea.supplierName = "ikea";
myProducts[0].productName = "chair";
myProducts[0].supplierName = ikea;
myProducts[1].productName = "table";
myProducts[1].supplierName = ikea;
Supplier javaBlackBelt = new Supplier();
javaBlackBelt.supplierName = "javaBlackBelt";
myProducts[2].productName = "OO Exam";
myProducts[2].supplierName = javaBlackBelt;
for(Product p: myProducts) {
System.out.println(p.productName+" is from "+p.supplierName);
}
}
}
Code :
public class Product {
String productName;
Supplier supplierName;
}
Code :
public class Supplier {
String supplierName;
}
Re: why am I getting NullPointerException.
You're initializing an array which can hold Product objects, it doesn't actually create any Product objects. You need to manually create them.
Re: why am I getting NullPointerException.
Quote:
myProducts[0].supplierName = ikea;
That code won't compile because ikea is not declared anywhere.
Re: why am I getting NullPointerException.
ok I fixed the code, I think it should look like this
Code :
/** Code removed by moderator */
Re: why am I getting NullPointerException.
Re: why am I getting NullPointerException.
ok, now I know what spoon-feeding is... I guess by removing my code is a sign that it was correct. Am asking b/c I would like to hear about a better implementation or at least a confirmation that it was correct
Re: why am I getting NullPointerException.
Quote:
Originally Posted by
mia_tech
ok, now I know what spoon-feeding is... I guess by removing my code is a sign that it was correct. Am asking b/c I would like to hear about a better implementation or at least a confirmation that it was correct
I owe you an apology. I didn't realize you were the OP posting your own code. I am so sorry. I thought someone else fixed the code. Please post it again and forgive my mistake.
Re: why am I getting NullPointerException.
Quote:
Originally Posted by
jps
I owe you an apology. I didn't realize you were the OP posting your own code. I am so sorry. I thought someone else fixed the code. Please post it again and forgive my mistake.
that was kind of strange, since I get no feed back weather my solution is correct or not. Anyways, here it goes again. If someone could take a look at it and let me know if is correct
Code :
public class Main {
public static void main(String[] args) {
Product[] myProducts = new Product[3];
Supplier ikea = new Supplier();
ikea.supplierName = "ikea";
Product chair = new Product();
chair.productName = "chair";
chair.supplier = ikea;
myProducts[0] = chair;
Product table = new Product();
table.productName = "table";
table.supplier = ikea;
myProducts[1] = table;
Supplier javaBlackBelt = new Supplier();
javaBlackBelt.supplierName = "javaBlackBelt";
Product ooProgramming = new Product();
ooProgramming.productName = "javaBlackBelt";
ooProgramming.supplier = javaBlackBelt;
myProducts[2] = ooProgramming;
for(Product p: myProducts) {
System.out.println(p.productName+" is from "+p.supplier.supplierName);
}
}
}
Code :
public class Product {
String productName;
Supplier supplier;
}
Code :
public class Supplier {
String supplierName;
}
Re: why am I getting NullPointerException.
Quote:
that was kind of strange, since I get no feed back weather my solution is correct or not.
Yes, removing the code was my mistake. It was late and I thought there was one name on the OP and ya, it shouldn't have been removed. I read over a hundred (and answer most) of questions a day on multiple forums, sometimes mistakes happen I guess.
Quote:
If someone could take a look at it and let me know if is correct
Does it compile? Does it run? Are there any error messages? Does it perform as expected? Writing programs is much more than getting code typed up. Troubleshooting and testing are part of the development process. The first thing to do after writing code is try to break it. Or to put it in better words, try to prove that the code is broken.
Re: why am I getting NullPointerException.
Does it compile and run correctly? As far as I can tell, it looks correct, but I didn't try running it.
I would suggest fully utilizing Object-Oriented design and define constructors to build complete objects. You should also declare your fields as private and use getters/setters to access the fields.
Here's an example getter/setter:
Code java:
public class Example
{
private String var;
public void setVar(String value)
{
var = value;
}
public String getVar()
{
return var;
}
}