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 8319623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:22:58+00:00 2026-06-08T22:22:58+00:00

I’ve been trying to get this right for 2 days and am about ready

  • 0

I’ve been trying to get this right for 2 days and am about ready to throw something …..

I have a JSONArray which I’m processing in a try/catch block but it doesn’t seem to pass the variable out at the end.

My code:

try{
    //Get the element that holds the results ( JSONArray )
    JSONArray  getresults = json.getJSONArray("results");
    //Loop the Array
    for(int i=0;i < getresults.length();i++){                       
        HashMap<String, String> map = new HashMap<String, String>();
        JSONObject e = getresults.getJSONObject(i);
        totalpass = e.getJSONObject(i).getString("ftotalpass");                         
    }                              

} catch(JSONException e)        {
    Log.e("log_tag", "Error parsing data "+e.toString());                                       
}

I’ve tried all manor of declaring the variable before, in, after the try/catch block but I just cannot get it to pass to the rest of my code.

What am I doing wrong?

  • 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-06-08T22:22:59+00:00Added an answer on June 8, 2026 at 10:22 pm

    Hmm…

    You can declare it outside. Such as

    JSONArray results = null;
    
    try {
         results = json.getJSONArray("results");
    }...
    

    This way you can access it outside, but be sure to use an if to see if it is null.

    If you don’t put JSONArray results = null the compiler will probably whine about the variable not being initialized.

    Further explanation:

    This happens because of the scope. When you declare inside the try, the scope of the variable ends when the try ends. When you declare inside an if, when that if ends, the variable can’t be accessed anymore. This is very useful for temporary variables where you only use them for comparison or better code readability, memory reuse, etc. (idk if this is even a word). Anyway, if your variable needs to be accessed everywhere inside a class, it may be better to declare as a field.

    I didn’t read this explanation, but it seems good. Take a look.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a jquery bug and I've been looking for hours now, I can't
I am trying to understand how to use SyndicationItem to display feed which is
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I am trying to loop through a bunch of documents I have to put
I don't have much knowledge about the IPv6 protocol, so sorry if the question

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.