Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I would like to inherrit the rooms variable just as foo is being inherited. I have searched around google and stackoverflow but couldn't find relevant posts. It may be a short description of the question but I don't think anything more is needed.
Code
import java.util.ArrayList;
class HomeAbstraction { // HomeAbstraction = Rooms; It's named like this to fit the rest of the project
float area;
public HomeAbstraction(float area) {
this.area = area;
}
}
class Home {
String foo = "bar";
ArrayList < HomeAbstraction > rooms = new ArrayList < HomeAbstraction > ();
// setter
public void setRooms(HomeAbstraction rooms) {
this.rooms.add(rooms);
}
}
class Department extends Home {
// constructor
public Department() {
foo = "foo"; // works fine
rooms; // should be pulled from parent but isn't working
rooms.setRooms(new HomeAbstraction(50)); // Not sure if I can access the setter from this child class
}
}
Error log
idk.java:25: error: cannot find symbol
rooms.setRooms(new HomeAbstraction(50));
^
symbol: method setRooms(HomeAbstraction)
location: variable rooms of type ArrayList<HomeAbstraction>
1 error
You can try:
rooms.add(new HomeAbstraction(50));
or
super.setRooms(new HomeAbstraction(50));
or
super.rooms.add(new HomeAbstraction(50));
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have an Android app that is recently published, and I'm receiving crash reports from Google Play Console. One of the crashes is strange. It happens in code similar to the following:
private final List<Item> items = new ArrayList<>();
public void addItem(Item item) {
items.add(item); // NullPointerException here!!
}
Apparently items is null when addItem is called, but it seems to me that it's impossible—I've initialized it at the declaration site. I can't imagine a situation in which this could happen, yet it happens a lot. Any idea?
Alright, psychic debugging time. The only way this could happen is if something is calling it as part of the object init, either during setup of other variables or an explicit init { ... } block.
You are almost certainly doing something like this elsewhere in the code:
import java.util.*;
class Main {
private static class Item { }
private final Item firstItem = setUpItemsAndReturnFirst();
private final List<Item> items = new ArrayList<>();
public void addItem(Item item) {
items.add(item); // NullPointerException here!!
}
private Item setUpItemsAndReturnFirst() {
Item first = new Item();
Item second = new Item();
addItem(first);
addItem(second);
return first;
}
public static void main(String args[]) {
new Main();
}
}
This contains your sample code, and will crash on that line with a NullPointerException, because setUpItemsAndReturnFirst() is being called before items is initialized when it's used to initialize firstItem.
I assume it's behind some sort of conditional, in your case, since it presumably doesn't crash every time or you'd never have released it. But this is the general idea.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to learn object oriented code in Java, and am following a tutorial. I am currently stuck trying to parse a string into my class. It is returning the following error:
Name cannot be resolved to a variable
I have a main file, called start.java, and the class I am trying to call is in a different file, called phone.java. Both are in a folder called src. Below is the start.java code (which is throwing the error)
package src;
public class Start {
public static void main(String[] args){
phone android = new phone(Name:"android 10");
System.out.println(android.getName());
}
}
And here is the class I am trying to call, in phone.java
package src;
public class phone{
private String name;
public phone(String name) {
this.name = name;
}
public String getName(){
return this.name;
}
}
Much thanks for your help
You need to remove the Name from new phone(Name:"android 10") and need to use new phone("android 10").
You just need to pass the value for the name, your constructor will bind it to the name variable.
Refer below code
public class Start {
public static void main(String[] args){
phone android = new phone("android 10");
System.out.println(android.getName());
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm new to this so sorry if i don't explain it great.
I have 2 class Interface and Depot.
I can call depot1.getName() and depot2.getName() in Instance class.
But I can't call them in Depot class as I am trying to check no instance of that class has the entered name:(tempname.equals(depot1.getName()) || tempname.equals(depot2.getName()))
Could it be because depot1 and depot2 haven't been created yet?
Here is getName
public String getName(){
return name;
}
I think it may have to do with the fact that depot2 may not exist so i tried:
(depot2 != null && tempname.equals(depot2.getName()))
but that still gives more erors and won't let me compile
I am getting the following error "cannot find symbol - variable depot1"
Can I use isInstance? https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isInstance%28java.lang.Object%29
Any help would be greatly appreciated, Thanks
Looks like your code don't have instance of depot1 create a instance using new like following snippet: This is not exact answer but it will help you.
package com.test;
class Depot
{
private String name;
public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
}
public class Test
{
public static void main( String[] args )
{
Depot depot1 = new Depot();
depot1.setName( "depot1" );
Depot depot2 = new Depot();
depot2.setName( "depot2" );
// Rest of code
//(tempname.equals(depot1.getName()) || tempname.equals(depot2.getName()))
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Can someone please explain how is possible, that method obtain(..) throws IllegalStateException for input ConfiguratorType.SKODA (the variable configurators contains {SKODA=null})? How can it be null, I do not understand why SkodaConfigurator.INSTANCE returns null. It should never be null or am i mistaken? The code is executed in servlet environment, Java 7.
Thank you
public class CarConfigurators {
private static Map<ConfiguratorType, CarConfigurator> configurators
= new EnumMap<ConfiguratorType, CarConfigurator>(ConfiguratorType.class);
static {
configurators.put(ConfiguratorType.SKODA, SkodaConfigurator.INSTANCE);
// ..
}
public static CarConfigurator obtain(ConfiguratorType type) {
CarConfigurator configurator = configurators.get(type);
if (configurator == null)
throw new IllegalStateException("Car configurator of type " + type + " is not registered.");
return configurator;
}
...
}
public class SkodaConfigurator extends CarConfigurator {
public static final SkodaConfigurator INSTANCE = new SkodaConfigurator();
...
}
public enum ConfiguratorType {
SKODA,
// ..
}
Static code cannot all run simultaneously, the various bits of static initialization going on have to happen in a given order. Clearly in this case, your static block which does configurations.put(...) is running before the static variable in SkodaConfiguration is initialized.
This is related to static initialization order.
I found this from another answer
public class Main {
{
System.out.printf("NON-STATIC BLOCK\n");
}
static{
System.out.printf("STATIC BLOCK\n");
}
public static Main m = new Main();
public Main(){
System.out.printf("MAIN CONSTRUCTOR\n");
}
public static void main(String... args) {
//Main m = new Main();
System.out.printf("MAIN METHOD\n");
}
}
Output :
STATIC BLOCK
NON-STATIC BLOCK
MAIN CONSTRUCTOR
MAIN METHOD
Please go through this : Java Static Initialization Order
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am currently creating a events recorder GUI program. Yet I am encountering a very difficult problem.
How should I store objects within an object within an object within an object?
For example,
I have an event.
This event has 4 category.
In the first category (Category A), there are 30 exhibition shows.
Within each show, there are 20 - 30 representatives. (Let's say 30 reps for the first show).
...
How can I store all these information in an arraylist? OR is there any other better idea?
Should I also apply Polymorphism to this one too?
Event --> Category A (first one out of the four) --> First Show out of 30 --> 1 rep out of 30 reps --> ... etc.
Thanks.
My confusion is that I would like to treat every single of these as an object. For example, category is an object. The show is an object. The reps is an object. My question is how can I store an object within an object within an object and so on? Thanks.
try this
Test.java
import java.util.List;
public class Test {
public List<Category> category;
}
import java.util.List;
Category.java
public class Category {
public List<Exhibition> exhibitionShow;
public void setExhibitionShow(List<Exhibition> exhibitionShow) {
this.exhibitionShow = exhibitionShow;
}
public List<Exhibition> getExhibitionShow() {
return exhibitionShow;
}
}
Exhibition.java
import java.util.List;
public class Exhibition {
public List<Representative> representative;
public void setRepresentative(List<Representative> representative) {
this.representative = representative;
}
public List<Representative> getRepresentative() {
return representative;
}
}
Representative .java
public class Representative {
//add method
}
I am not sure what is your confusion, you need to do something like this
class Event{
Category[] categories;
}
class Category{
ArrayList<Show> shows;
}
class Show{
ArrayList<Representative> reps;
}
//.. and so on.
I think you have the idea. My idea is create one public method.