This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I wrote a simple program in JAVA having 4 classes:
Main - > CarFaxApp
GUI / Logic - > CarFaxFrame
Object -> Car
Validator -> CarFaxValidator
Once the program is running and I start to enter the necessary data in the textbox's for the car (String vin, String make, String model, int year). Everything works fine, but when I hit buttonADD(), I get a error.
private HashMap<String,Car>hmCar; //Hashmap to hold Cars, the key pair value consists of (Vin, Car Object)
The error happens on hmCar.put(car.getVin(), car); which I dont understand why, I tried debugging up to the point and car does hold the correct values.
Here a screen shot of the error:
the error is NullPointException, are you sure you do this
hmCar = new HashMap<String, Car>();
before you put the entry in.
Related
This question already has answers here:
How can I count occurrences with groupBy?
(6 answers)
Counting occurrences in a list with Java 8 [duplicate]
(1 answer)
Closed 1 year ago.
I have a record for Employees:
public record Employee(
String name,
String id,
string imm_supervisor,
){}
I have a stream of Employee objects, and I want to extract a hashmap that gives me a mapping between each imm_supervisor and the amount of employees that the supervisor directly supervises.
I'm frankly confused by documentation, and I've been getting errors not knowing how stream this properly. This is as close as I got:
Map<String, Integer> mp = streamWithEmployees.map(x->supervisorofEmployee(x)).collect(groupingBy(/*String*/, counting()));
supervisorofEmployee(employee) returns a string, imm_supervisor. I'm not sure how to grab the returned strings from the map and put them into the map.
Hope this makes sense.
This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 1 year ago.
This post was edited and submitted for review 12 months ago and failed to reopen the post:
Original close reason(s) were not resolved
-When I use toString to print: System.out.println(animal);
I get this : Animal{name='Shark', weight=30, speed=40}
What I want is : Animal {name = 'Shark',
weight = 30,
speed = 40}.
How I can force I-IDEA make what I want automatically?
You can generate toString() method and edit that
This question already has answers here:
The left-hand side of an assignment must be a variable
(6 answers)
"The left-hand side of an assignment must be a variable" problem with charAt
(4 answers)
How do I edit my program in order to assign a variable to an array list
(2 answers)
Closed 2 years ago.
This post was edited and submitted for review 11 months ago and failed to reopen the post:
Needs details or clarity Add details and clarify the problem by editing this post.
In the following loop how may I create a new List<>(), but I have a problem in the moment I try to set a value:
ListlistOfString = new ArrayList()<>;
listOfString.set(index, value);
Can anyone explain me how does it work?
Unlike arrays, you cannot do array[i] = val. You should use ArrayList.set:
// this must be set
announcementDTO.set(j, new ArrayList<>());
// this can still be get
announcementDTO.get(j).setIdShop(shopBean.getId());
announcementDTO.get(j).setNombreComercio(comercioBean.getNombre());
announcementDTO.get(j).setRazonSocial(comercioBean.getRazonSocial());
This question already has answers here:
Java associative-array
(15 answers)
Closed 2 years ago.
I'm beginner in Java environment, accustomed to PHP.
And in Php we can create an array like the code below :
$array['params1'] = 'the first param';
$array['params2'] = 'the second param';
And when i will output $array['params1'] it will be 'the first param'.
But i do not find any similar solutions in Java, do you know something similar ?
Thanks in advance
As #mrblewog said, you might want to readup on data structures and the syntax in Java as it is quite different than php.
To give you an Example:
// Key Value
HashMap<String, String> map = new HashMap<>();
map.insert("key1", "value1");
map.get("key1"); // returns "value1"
If you want to store other objects than Strings you will need to change the generic types (written in the <X, Y>).
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Assigning in Java?
(5 answers)
Closed 3 years ago.
PS: This question is different than others because I need to know exactly what code to write in order to prevent the variable from changing.
I have this example of code to demonstrate my problem:
float[][] one = {{0}};
float[][] two = one;
System.out.println(two[0][0]);
one[0][0] ++;
System.out.println(two[0][0]);
int three = 0;
int four = three;
System.out.println(four);
three ++;
System.out.println(four);
The output is:
0.0
1.0
0
0
For some reason, when I change float[][] 'one', 'two' automatically changes as well.
However, when I performed the same task but for a different data type int, then the code works as intended.
What is causing this variable to drag the other one with it?
Why doesn't it happen for the int type?
How do I fix it?