Why wont this shitty Java code work? Why can't I access the property of a class?

Robert Kelly
Robert Kelly

Why wont this shitty Java code work? Why can't I access the property of a class?

Adrian Sanchez
Adrian Sanchez

Not sure if bait.

William Carter
William Carter

post the rest of your code.

Elijah Howard
Elijah Howard

No (You)s for you.

Caleb James
Caleb James

Its an honest question. Why the fuck wont it let me access the property if I point an Object to the fucking shit

The hashmap contains objects of a class which has a protected property called name hence hashMap.get(1).name is valid.

Evan Lee
Evan Lee

Show the declaration / initialization of the Map or gtfo

David Russell
David Russell

java sux

use bash instead

Anthony Scott
Anthony Scott

Actually, you just aren't casting it to your class type correctly

Cooper Clark
Cooper Clark

Map<Integer,LeClass> hashMap = new HashMap<Integer,LeClass>();

hashMap.put(1, new LeClass("faget"));

class LeClass{

protected String name;
}

Jason Martin
Jason Martin

Of course the LeClass contains a constructor that sets the name as well but i forgot to include it in my post

Brayden Reyes
Brayden Reyes

What?

Cameron Thomas
Cameron Thomas

yep you're definitely not casting right.

Object can't access the attributes, so you have to declare the 'obj' variable as LeClass, otherwise it can't see the fields.

Owen Ross
Owen Ross

In the OP you are retrieving a value from from your map but assigning it to a variable of type Object. Object does not have a name property, there is no way of knowing at compile time that name exists, so that's why you're getting an error.

Why don't you just declare obj as type LeClass instead of Object?

Nathan Rivera
Nathan Rivera

Ah i see, thanks

Angel Watson
Angel Watson

example.

also don't use protected fields and access directly, use private fields and getters/setters. eclipse even has boilerplate generators for it.

Cooper Foster
Cooper Foster

surely i'm not the only one who dislikes using getters and setters for every field, especially when there's no get/set logic to be done nor will there ever be. basic record-type style objects should just expose, imo

Wyatt Davis
Wyatt Davis

you might not like it, but it's good practice for java.

Adrian Jackson
Adrian Jackson

Yeah, i dont use getters and setters for primitive types

Nathaniel Campbell
Nathaniel Campbell

thing.setValue(thing.getValue() + other.getValue());
vs.
thing.value += other.getValue;
vs.
defining yet another method for changing a field...

Nicholas Jenkins
Nicholas Jenkins

There are some cases where it's alright to make a public variable and access it directly, and then there are other cases where you're trying to figure out why your variable is fucked up and you spend hours digging though decade old code to find that someone accessed it directly and added some random ass value to it to bypass some bug that they were too lazy to properly fix.

Chase Howard
Chase Howard

Correct me if I'm wrong, shouldn't you check if the key exists in the first place? (this is a runtime error, irrelevant to the compile-time one as in OP's pic related)

E.g. In C#
if(myDic.hasKey("shitStain")) myDic["shitStain"].shittyDateField = DateTime.Now;

phone CS shitposting

Connor Bennett
Connor Bennett

shouldn't you check if the key exists in the first place
Why? I already know it exists ;^)

The less code the better

Ethan Reyes
Ethan Reyes

Enjoy your NullPointerExceptions

Levi Sullivan
Levi Sullivan

Also, dude/t there is no name field in Object, you have to downcast to your type which is the type of Value in Key-Value pair in your "god-tier" hash map. Subtypes expose supertypes but not vice-versa. Gotta down cast bruh.

Ma man.

all-original Syrian CS shitposting continues

Disable AdBlock to view this page

Disable AdBlock to view this page

Confirm your age

This website may contain content of an adult nature. If you are under the age of 18, if such content offends you or if it is illegal to view such content in your community, please EXIT.

Enter Exit

About Privacy

We use cookies to personalize content and ads, to provide social media features and to analyze our traffic. We also share information about your use of our site with our advertising and analytics partners.

Accept Exit