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

  • Home
  • SEARCH
  • 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 7671897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:09:44+00:00 2026-05-31T16:09:44+00:00

If the compiler is going to complain that a String object is not initialized

  • 0

If the compiler is going to complain that a String object is not initialized then is it appropriate to check in my method whether the String object is null or not?

Is there a place that the compiler will not check that for me?

  • 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-31T16:09:45+00:00Added an answer on May 31, 2026 at 4:09 pm

    Two issues here – the one in the title and the one you seem to be asking.

    Initialisation

    The compiler is complaining about “initialisation” not whether or not it is null.

    Initialisation is simply that you have given it a value – either by setting it to point to a specific String object or by explicitely setting it to null.

    Prior to initialisation the value of the variable is unknown… roughly speaking it is whatever value just happens to have been left in that memory address by the last thing that used it.

    However you don’t need to worry about checking for initialisation at run time – the compiler won’t let you get to the point where it is an issue.

    Null Reference Checks

    Your headline question of “do I have to check whether a String object is null?” however is a slightly different issue and that you do have to consider at run time.

    There are three ways to handle null references.

    1. Check that the references are not null in your logic (i.e. in an if statement)
    2. Put the check in an assertion
    3. Put the code in a try/catch block with an explicit catch for java.lang.NullPointerException
    4. Ignore the issue completely

    The last option simply means that if a null is used then the exception will run all the way back to the entry point of the thread into your code and terminate the thread.

    It is because of this it is always wise to put a global catch around each thread entry point for your code – one which as a minimum reports that an unhandled exception occurred with plenty of information to help you work out what happened (probably before terminating the application as it is in an unplanned & unexpected state).

    Which of the above options you do really depends on the likelihood of there being a null pointer at that point. If the reference was created and managed in the current class and you know that it is “impossible” for it to be null, then option 4 is valid.

    Conversely if you are creating generic code that other developers (possibly outside of your control) might call and particularly where the null could have long-term detrimental effects to your object (e.g. you may use it to set something that will only break your code much later) then option 1 is worthwhile.

    Option 2 is useful if you want to be able to turn it off after testing and also to make it clear in the code that the test isn’t part of the algorithm, just a safety measure

    Option 3 is rare, but can be used if you have a lot of variables being passed in with a slim chance of them being an issue, but if they are then the problem will manifest during the call.

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

Sidebar

Related Questions

Not really sure whats going on here, whether Im just being a fool or
The C# 4.0 compiler does not complain about this (not even a warning): if(10.0
Compiler error CS0283 indicates that only the basic POD types (as well as strings,
Compiler Error Keyword 'this' is not available in the current context delegate void CallBack(int
I'm going to try to write a compiler for a dynamic language. Preferably to
I'm writing a compiler in ocaml, using ocamllex/yacc. Things are going well, but I've
I was going through some code today and saw this: x.add(getResources().getString(R.string.none)); x.add(getResources().getString(R.string.today)); x.add(getResources().getString(R.string.tomorrow)); ...
I have a compiler homework question that wants me to draw a DFA for
I'm making a mess of creating an object in a method and returning it
I was recently going through some code and considering whether I need to be

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.