0

I have another class, which needs to update progressbar in my UI while doing process. I tried 2 methods, both sets my ProgressBar variable to null, so I ended up getting NullPointerException all the time:

Method 1)

MyClass tmp = new MyClass();
tmp.setProgressBar(myprg);    // myprg is ProgressBar variable and already have value from findViewById function

Then in MyClass:

public void SetProgressBar(ProgressBar prg)
{
localprg = prg;   // I put breakpoint here, it's null after assignment
}

Method 2)

MyClass tmp = new MyClass(container.getContext());
tmp.findProgressBar();

in MyClass:

public class void findProgressBar()
{
localprg = (ProgressBar) ((Activity)c).findViewById(R.id.progress_bar);
}

Both failed, both makes localprg = null.

  • IS myprg is also null? – Giru Bhai May 26 '14 at 15:37
  • myprg in my own class (caller class) no it's not null. But in MyClass, it becomes null suddenly –  May 26 '14 at 15:40
  • So first get instance of myPrg – Giru Bhai May 26 '14 at 15:42
  • It gets nulledWhen You leave SetProgress bar, or when You exit findProgressBar()? In Java all is passed as value, means, ProgressBar is a local value, not more alive when SetProgressbar leaves, getting null, YOu assigned the value variable via reference to localprg, means, this also gets null then. – icbytes May 26 '14 at 15:42
  • No, localprg is a local variable for whole class, not local variable for that function. Any @GiruBhai: I already have instance of myPrg on my own class (caller class) so I pass it to that function in another class and it becomes null there, so it assigns null to an already null local variable –  May 26 '14 at 15:58
  • @icbytes: re-checked it, it's not local function variable, it's variable for whole class –  May 26 '14 at 15:59
  • The parameter is function call scope ONLY. It gets null after the function is left, but You assigned it pointing to localprg, what means, it also gets null.Create some other lines after SetprogressBar, debug them and spot WHEN localprg gets null. It most likely is at the end of SetProgressBar . – icbytes May 26 '14 at 16:09
  • @icbytes I added a lot of random code after bprogress value assignment function, stil it was null before function ending. –  May 26 '14 at 16:24
  • @icbytes: in SetProgressBar function, even parameter that has been passed to is null, but it's not null when I'm calling function –  May 26 '14 at 16:25
  • What? Explain in Detail please. Caller method passes null but Inside called method is no more null? – icbytes May 26 '14 at 17:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54465/discussion-between-user3404070-and-icbytes). –  May 26 '14 at 17:48

1 Answers1

0

As i said in my comments. In java all is passed via value. Not via reference. The parameter inside your method signature works as if you had defined it locally in the method stub. Try to change the parameter inside method signature to AtomicReference if You really need to pass and change an UI object inside a class attribute. This is not the proper way. But here is still a link, which strengthens my answers:

Pass object by reference in Java

As it seems, You want to increase a progress bar fluently at runtime. Your class attribute seems to take care about calculating the value, which is used to indicate the progress of the progress bar. This is not the right way.

a) You created cross reference by passing parent object to child attribute. This is not good practice and creates strong coupling.

b) Heavy calculations should be run in an own worker thread ( non ui thread ) , which then announces ui thread via events with event-arguments. The eventsubscriber then may decide how to react to that event. You can read about it as "java event handling" or "java observer pattern".

c) Threads ? An own huge topic.

Community
  • 1
  • 1
icbytes
  • 1,831
  • 1
  • 17
  • 27