Re: help: Java is skipping a constructor!

  • From: "Alex Hall" <mehgcap@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Mon, 16 Nov 2009 21:58:52 -0500

Yes, I understand exactly why it was doing that. My problem was that I was looking at it linearly, forgetting that, no matter where you declare a variable, it will be allocated first in execution. Thanks again.



Have a great day,
Alex
New email address: mehgcap@xxxxxxxxx
----- Original Message ----- From: "Sina Bahram" <sbahram@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Monday, November 16, 2009 20:24
Subject: RE: help: Java is skipping a constructor!


Just so we're clear ... You do understand why this was not working, right?

I just want to point out that the reason I was able to find this in under
three minutes is not magic. I started from first principles, which is what
you should do if you don't wish to pull your hair out every time.  I knew
something had to be null-pointer excepting before the constructor, and I
verified everything before it with a few simple printlines, which meant I
had to think of what loads before the constructor ... And obviously that
includes the instances variables being allocated .... So I just found your
instance variables, and saw the issue.

If you can always start from first principles, you will surpass about 80% of
programmers off the bat, because most folks just attack problems randomly
until they solve it.

Take care,
Sina


-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex Hall
Sent: Monday, November 16, 2009 6:48 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: help: Java is skipping a constructor!

That did it, thanks so much!! I knew it had to be something simple
that I was just not seeing (or hearing, as the case may be). It seems
to be working now... Now to go find the logic errors I know I made!

On 11/16/09, Sina Bahram <sbahram@xxxxxxxxx> wrote:
Aha, this is very easy to fix ... Works over here now.

A more evil person would end the email there, *grin*.

Examine your BankImpl class.

What do you see wrong with your instance variables?

You are assigning them to things, but there is no class when that occurs,
because they get run before any constructor.

The null pointer error comes from exactly where it says essentially ...
Your
assigning available.length to l, but there is no available yet ... No
constructor has run, no values have been passed.

You do this a few more times as well.

My advice:

First, group all your instance variables at the top ... None of this,
defining a constructor then after the constructor defining more variables
stuff.

Secondly ... You only define them in the class, but you assign them in
your
constructor.

So you have:

int[] available;
int l;
int customers;
int totalThreads; //how many threads there are
int totalResources; //how many resources there are
int[] order;
int[][] maximum;
int[][] allocation;
int[][] need;

And then you can assign them in your constructor:

l=available.length; //testing to see if available is null
order=new int[Customer.COUNT]; //order[0] is the process to be run first,
[1] is the process to run 2nd, //and so on
maximum=new int[Customer.COUNT][available.length]; //[process] is a row
for
process, holding the process's needs for //each resource (resources are
the
columns)
allocation=new int[Customer.COUNT][available.length]; //same as max, but
how
much of each resource is currently //allocated to each process instead of
how much it might need of a //resource
need=new int[Customer.COUNT][available.length]; //same, but how much a
process still needs of a resource

Don't bother zeroing out ints: the standard states they get assigned 0 ...
Java is a mature language and doesn't fill your primative types with junk
like C/C++ *grin*.

Anyways, that solves your problem ... Works fine over here.

Take care,
Sina

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex Hall
Sent: Monday, November 16, 2009 5:29 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: help: Java is skipping a constructor!

Try this bankimpl file instead. I have simplified it (I tried to just
rewrite the constructor) but it still hits the same error. I do not even
care if the available array has values in it, I just want it to be the
right

size. It compiles for me, but when you run it, it still hits that null
pointer exception like available is not getting set to the resources array
size.


Have a great day,
Alex
New email address: mehgcap@xxxxxxxxx
----- Original Message -----
From: "Sina Bahram" <sbahram@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Friday, November 13, 2009 22:10
Subject: RE: help: Java is skipping a constructor!


Send me the interface this thing implements.

And honestly ... Can you just zip it up and send it to me?

Take care,
Sina

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex Hall
Sent: Friday, November 13, 2009 6:38 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: help: Java is skipping a constructor!

Okay, here it is. The code executes until it hits "public BankImpl(int[]
res), where it skips all lines until "int l=available.length". I never
see
any of the print statements, and my prof says that JGrasp shows the
execution bypassing the constructor entirely, even though the main method
(in another file) creates a new BankImpl object in the usual way.

public class BankImpl implements Bank{
public BankImpl(int[] res){
System.out.println("constructing bank object");
int[] available=new int[res.length];
int i;
for(i=0;i<res.length;i++){
available[i]=res[i];
System.out.println(available[i]+", "+available.length);
}
//available=res; //resources with which this bankimpl object starts
}//constructor

int l=available.length;
int customers=0;
int totalThreads; //how many threads there are
int totalResources; //how many resources there are
int[] order=new int[Customer.COUNT]; //order[0] is the process to be run
first, [1] is the process to run 2nd, //and so on
int[][] maximum=new int[Customer.COUNT][available.length]; //[process] is
a
row for process, holding the process's needs for //each resource
(resources
are the columns)
int[][] allocation=new int[Customer.COUNT][available.length]; //same as
max,

but how much of each resource is currently //allocated to each process
instead of how much it might need of a //resource
int[][] need=new int[Customer.COUNT][available.length]; //same, but how
much

a process still needs of a resource


Have a great day,
Alex
New email address: mehgcap@xxxxxxxxx
----- Original Message -----
From: "Sina Bahram" <sbahram@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Friday, November 13, 2009 17:04
Subject: RE: help: Java is skipping a constructor!


Post code

Take care,
Sina


-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex Hall
Sent: Friday, November 13, 2009 4:05 PM
To: programmingblind
Subject: help: Java is skipping a constructor!

Hi all,
I have a java program here, and the file I have to write is the class
for an object. This class implements an abstract class, which is
already written. A third file holds the main() which, among other
things, creates an object from the class I have to write. Of course,
this being a class, I have a public constructor. Here is the odd
thing: the constructor is being bypassed completely. A sighted person
ran my program in a Java IDE, JGrasp, and told me that the execution
goes along normally until it hits the constructor. Once there it skips
down past the end of the constructor and continues running like
normal. Has anyone ever encountered this before, in any form? My prof
and I agree that the constructor looks good and should not have any
problems, and neither of us can figure out why, or how, the program
can just skip the whole constructor, almost like there is a ocndition
telling the object to not be created, but that is not the case here.
Thoughts? If you want to see the constructor I can post it, but I am
hoping that this is a common enough thing that someone can recognize
the signs and tell me what I might be doing wrong. Thanks!!

--
Have a great day,
Alex
My email is now: mehgcap@xxxxxxxxx
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind




--
Have a great day,
Alex
My email is now: mehgcap@xxxxxxxxx
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: