Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 874995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:11:41+00:00 2026-05-15T11:11:41+00:00

Basically what I mean is like this: List<String[]> routes = (List<String[]>)application.getAttribute(routes); For the above

  • 0

Basically what I mean is like this:

List<String[]> routes = (List<String[]>)application.getAttribute("routes");

For the above code, it tries to get an attribute named “routes” from the JSP implicit object – application. But as everyone knows, after this line of code, routes may very well contains a null – which means this application hasn’t got an attribute named “routes”.

Is this “casting on null” good programming practice in Java or not?

Basically I try to avoid exceptions such as java.io.InvalidCastException

I reckon things like this are more as “heritage” of Java 1.4 when generic types were not introduced to this language. So I guess everything stored in application attributes as Objects (Similar to traditional ArrayList). And when you do “downcast”, there might be invalid casts.

What would you do in this case?

Update:

Just found that although in the implicit object application I did store a List of String arrays, when I do this :

List<double[]> routes = (List<double[]>)application.getAttribute("routes");

It doesn’t produce any error… And I felt not comfortable already…

And even with this code:

out.print(routes.get(0));

It does print out something strange :

[Ljava.lang.String;@18b352f

Am I printing a “pointer to String”? I can finally get an exception like this:

out.print(routes.get(0)[1]);

with error :

java.lang.ClassCastException: [Ljava.lang.String;

Because it was me to add the application attribute, I know which type should it be cast to. I feel this is not “good enough”, what if I forget the exact type?
I know there are still some cases where this sort of thing would happen, such as in JSP/Servlet when you do casting on session attributes. Is there a better way to do this?

Before you say:”OMG, why you don’t use Servlets???”, I should justify my reason as this: – because my uni sucks, its server can only work with Servlets generated by JSP, and I don’t really want to learn how to fix issues like that. look at my previous question on this
, and this is uni homework, so I’ve got no other choice, forget all about war, WEB-INF,etc, but “code everything directly into JSP” – because the professors do that too. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:11:42+00:00Added an answer on May 15, 2026 at 11:11 am

    Java uses type erasure to store collections. That is, the compiler enforces type safety, but reduces collections down to collections of Objects. So when you pull a list object out of your bean, it’s a List<Object> rather than a List<Double>.

    Because it’s a collection, you can get the nth element (as you’re doing). However the object you get is not what you expect. So when you pull out an element from the collection and cast it (to Double instead of String), the runtime will finally object.

    [Ljava.lang.String;@18b352f is a reference to a String array, btw. (the ‘[‘ indicates an array).

    For this reason (and others) I don’t like storing collections of collections of types. I would prefer to create a particular object type (a container) and then ask that object for the elements I require. So you’ll only have to cast once upon pulling the object from the bean, and then the complexity of handling arrays of doubles within lists is managed within this object (you can ask the container object to find elements for you, the logic of extracting/casting is in one place etc.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.