• Advertisement
Sign in to follow this  

ByteBuffer[0].asFloatBuffer() issues

This topic is 978 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I've got two problems

1. My console and logcat don't output anything anymore. Eclipse IDE / Android Java

   I've done  adb kill-server ,  adb start-server on command line to no success, which means debugging is close to impossible. Hope someoone knows how i can get outputs back to console and logat.  Lots of thanks

 

2. More importantly, this crash problem:  My code crashes at the starred *** line below  (Written in Android Java OpenGL ES)

   

    No output to both console and logcat has made it more difficult but nonetheless i still managed to trace the crash to the starred line. While trying to find solution to the console/logcat problem i'm hoping someone using both experience of similar coding and intuition might be able to help

    The crash happens on this line (3 code lines from bottom), : ObjectBuffers[j].DBufferD[ut] = vbbcD[0].asFloatBuffer(); //****crashes here *****

    so how do i define asFloatBuffer on this line

 

    I will appreciate any help to resolve the 2 promblems

    Many thanks

setupByteBuffer( vertIterator.next(), vertIterator.previousIndex()  );

......

   public void setupByteBuffer( CreateObjVertices vertices, int j  ){
	   
	    if( vertices.objectArrayLength > 0 ){  
	           floatArray = new float[vertices.objectArrayLength*3];
		   
		   for(int k=0, s=0; k<vertices.objectArrayLength; k++, s=s+3){		   
			   floatArray[s+0] =  vertices.objectVertexArray[k].x;
			   floatArray[s+1] =  vertices.objectVertexArray[k].y;
			   floatArray[s+2] =  vertices.objectVertexArray[k].z;
		   }/**/
		   
		   ByteBuffer vbbc = ByteBuffer.allocateDirect(vertices.objectVertexArray.length*3 * 4);
		   vbbc.order(ByteOrder.nativeOrder());              
		   ObjectBuffers[j].DBuffer = vbbc.asFloatBuffer();  
		   ObjectBuffers[j].DBuffer.put(floatArray);         
		   ObjectBuffers[j].DBuffer.position(0);            
	   }
	   else if( vertices.objectArrayLengthDD != null ){
			floatArrayDD = new float[vertices.objectVertexArrayDD.length][];
			
			for(int ut=0; ut<floatArrayDD.length; ut++){
				floatArrayDD[ut] = new float[vertices.objectVertexArrayDD[ut].length*3];
				
				for(int s=0, st=0; st<vertices.objectVertexArrayDD[ut].length ; s=s+3, st++){
				   floatArrayDD[ut][s+0] =  vertices.objectVertexArrayDD[ut][st].x;
				   floatArrayDD[ut][s+1] =  vertices.objectVertexArrayDD[ut][st].y;
				   floatArrayDD[ut][s+2] =  vertices.objectVertexArrayDD[ut][st].z;
				}
			}	

			for(int ut=0; ut<floatArrayDD.length; ut++){
                                              //EDIT CODE (below current code edited from initial post)

			   ByteBuffer vbbcD = ByteBuffer.allocateDirect(vertices.objectVertexArrayDD[ut].length*3 * 4 *4);
			   vbbcD.order(ByteOrder.nativeOrder());              
			   ObjectBuffers[j].DBufferD[ut] = vbbcD.asFloatBuffer(); //****crashes here *****
			   ObjectBuffers[j].DBufferD[ut].put(floatArrayDD[ut]);         
			   ObjectBuffers[j].DBufferD[ut].position(0);             
		     }			
	  }
   }
 

Share this post


Link to post
Share on other sites
Advertisement
Any error messages or exceptions?

First guesses for me -- I haven't really studied the code -- are:

Wrong type of data in vbbcD[0], so asFloatBuffer() doesn't work
null pointers in any one of the six object lookups that take place on that line
exceeding the length of any of those arrays

Share this post


Link to post
Share on other sites

Any error messages or exceptions?

 

still no success in getting logcat or console to print crash exception message. Been googling and working on this for several hours but can't still get output (miserable mellow.png)

 

Wrong type of data in vbbcD[0], so asFloatBuffer() doesn't work

 

edit:    vbbcD is of type ByteBuffer                                                                                             3 (x,y,z)  *  4(reference) * 4(integer)

ByteBuffer vbbcD = ByteBuffer.allocateDirect(vertices.objectVertexArrayDD[ut].length*3 *4 * 4);

EDIT: manage to get console to work (partially) and also managed to grabbed some exception output before it scrolled off to disappear

08-19 04:56:01.197: E/AndroidRuntime(9353): FATAL EXCEPTION: main08-19 04:56:01.197: E/AndroidRuntime(9353): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.liveideas/com.example.liveideas.Convert}: java.lang.NullPointerException
08-19 04:56:01.197: E/AndroidRuntime(9353):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-19 04:56:01.197: E/AndroidRuntime(9353):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-19 04:56:01.197: E/AndroidRuntime(9353):  at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-19 04:56:01.197: E/AndroidRuntime(9353):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-19 04:56:01.197: E/AndroidRuntime(9353):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 04:56:01.197: E/AndroidRuntime(9353):  at android.os.Looper.loop(Looper.java:137)
08-19 04:56:01.197: E/AndroidRuntime(9353):  at android.app.ActivityThread.main(ActivityThread.java:4921)
08-19 04:56:01.197: E/AndroidRuntime(9353):  at java.lang.reflect.Method.invokeNative(Native Method)
08-19 04:56:01.197: E/AndroidRuntime(9353):  at java.lang.reflect.Method.invoke(Method.java:511)
08-19 04:56:01.197: E/AndroidRuntime(9353):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
08-19 04:56:01.197: E/AndroidRuntime(9353):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
08-19 04:56:01.197: E/AndroidRuntime(9353):  at dalvik.system.NativeStart.main(Native Method)
 

  

Edited by alwaysBiased

Share this post


Link to post
Share on other sites

vbbcD is of type ByteBuffer
 
ByteBuffer vbbcD[];
...
vbbcD = new ByteBuffer[1];


That creates an object that has an array. It doesn't actually put stuff in the array yet.

The line vbbcD[0] = ByteBuffer.allocateDirect(vertices.objectVertexArrayDD[ut].length*3 * 4); puts something in there, creating a ByteBuffer with a length of some multiple of 12. Might be zero, so you might want to print that value to double check.

Then you set the ordering, so vbbcD[0] isn't the null value here.

The next line does quite a lot:

ObjectBuffers[j].DBufferD[ut] = vbbcD[0].asFloatBuffer();

Since you are debugging through printing to console, add a big println statement above this to print the values of j and ut, and validate that ObjectBuffers is not null, ObjectBuffers[j] is not null, and that ObjectBuffers[j].DBufferD is not null. Since I don't see their sizes in the code, you might want to also print out how big those both ObjectBuffers and DBufferD are to ensure both j and ut are within bounds.

Share this post


Link to post
Share on other sites

 

The line vbbcD[0] = ByteBuffer.allocateDirect(vertices.objectVertexArrayDD[ut].length*3 * 4); puts something in there, creating a ByteBuffer with a length of some multiple of 12. Might be zero, so you might want to print that value to double check.

Then you set the ordering, so vbbcD[0] isn't the null value here.

 

m thanks

I realized thats true. ie vbbcD only needs to be set to be able to sufficiently hold the right amount of bytes so i edited the code to:

 

                                                                                                                                                 3 (x,y,z)  *  4(reference) * 4(integer)

ByteBuffer vbbcD = ByteBuffer.allocateDirect(vertices.objectVertexArrayDD[ut].length*3 *4 * 4);

Still it crashes. After reading the crash dump to logcat I'm not even sure how it is related to code 

 

But i also printed j and ut and i got

 

V/TAG   (14311):  j  17   ut  0

 

meant the 17th object crashed at the 0th iteration of ut  (both valid)

 

Do you know if 3*4*4 bytes is sufficient for x,y,y,  referencing an integer?

Share this post


Link to post
Share on other sites
 

it all boils down to the fact that the argument in ByteBuffer.allocateDirect(.....) is not set up right and vbbcD.asFloatBuffer() needs refining:

.

           ByteBuffer vbbcD = ByteBuffer.allocateDirect(vertices.objectVertexArrayDD[ut].length*3 * 4 *4);
           vbbcD.order(ByteOrder.nativeOrder());
           ObjectBuffers[j].DBufferD[ut] = vbbcD.asFloatBuffer(); //****crashes here *****

.

Does anyone, based on experience of setting up ByteBuffer for arrays of array-> xxx[ ][ ] know the correct code?

 

EDIT: SOLVED

Since you are debugging through printing to console, add a big println statement above this to print the values of j and ut, and validate that ObjectBuffers is not null, ObjectBuffers[j] is not null, and that ObjectBuffers[j].DBufferD is not null. Since I don't see their sizes in the code, you might want to also print out how big those both ObjectBuffers and DBufferD are to ensure both j and ut are within bounds. 

 

 

I'm not proud to write this but it turns out 'frob' was right when he wrote that i should check ObjectBuffers[j].DBufferD is not null. Even though i was so sure it wasn't null initially.

But i soon noticed i hadn't 'newed' the ObjectBuffers[j].DBufferD array.  So when I added 

ObjectBuffers[j].DBufferD = new FloatBuffer[floatArrayDD.length];  just below the inner else if(...), the whole problem was completely solved to the t

 

Share this post


Link to post
Share on other sites

I'm not proud to write this but it turns out...

No need to be embarrassed.

Those two items -- buffer overruns and null pointers -- are among the most common programming bugs.

Any veteran programmer has run across them thousands of times over their careers.

The harder part is tracking them down and fixing them. No need to be embarrassed in asking for help looking for them, sometimes entire teams can spend months looking for the source of various data corruptions and bugs. Every project I've been on has had at least one "nemesis bug" that haunts the team for weeks or months, with everyone hunting for that elusive flaw.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement