Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 404
Registered: ‎05-31-2011
My Device: curve
My Carrier: devel

java.lang.ArithmeticException

hi, i get an java.lang.ArithmeticException when i add something to the mainscreen,

delete_manager();
         new Thread()
            	  {
            	        public void run() {
            	                try {
 pillone_btn(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } UiApplication.getUiApplication().invokeLater(new Runnable() { public void run(){ managerin.add(hgm); try{
 System.out.println(managerin.getFieldCount());
 System.out.println(foreground.getFieldCount()); foreground.add(managerin);
  System.out.println(foreground.getFieldCount()); }catch(Exception das) { System.out.println(das); } try {

synchronized(UiApplication.getApplication().getEventLock()){ add(foreground); } }catch(Exception das) { System.out.println(das); } } });

the breakpoint continue to add(foreground) and i get this exception

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: java.lang.ArithmeticException

Is foreground a standard RIM manager or one of your own?  Can we see it?

 

Also when you run this in the debugger you should be an indication of the line it failed on and, if you drill down the Exception, some detail text.  Can you give us this information?

Developer
Posts: 404
Registered: ‎05-31-2011
My Device: curve
My Carrier: devel

Re: java.lang.ArithmeticException

import net.rim.device.api.ui.Graphics;

public class ForegroundManager extends NegativeMarginVerticalFieldManager {
    
    public ForegroundManager() {
        super( USE_ALL_HEIGHT | VERTICAL_SCROLL );
    }
    
    protected void paintBackground( Graphics g )
    {
        int oldColor = g.getColor();
        try {
            g.setColor( 0xDDDDDD );
            g.fillRect( 0, getVerticalScroll(), getWidth(), getHeight() );
        } finally {
            g.setColor( oldColor );
        }
    }
}

 

Developer
Posts: 404
Registered: ‎05-31-2011
My Device: curve
My Carrier: devel

Re: java.lang.ArithmeticException

import net.rim.device.api.ui.Graphics;

public class ForegroundManager extends NegativeMarginVerticalFieldManager {
    
    public ForegroundManager() {
        super( USE_ALL_HEIGHT | VERTICAL_SCROLL );
    }
    
    protected void paintBackground( Graphics g )
    {
        int oldColor = g.getColor();
        try {
            g.setColor( 0xDDDDDD );
            g.fillRect( 0, getVerticalScroll(), getWidth(), getHeight() );
        } finally {
            g.setColor( oldColor );
        }
    }
}

 and this is the NegativeMarginVerticalFieldManager.java class

Developer
Posts: 404
Registered: ‎05-31-2011
My Device: curve
My Carrier: devel

Re: java.lang.ArithmeticException

package Custom;


import net.rim.device.api.ui.*;

/**
 * A basic vertical field manager that supports negative vertical margins
 * It also supports horizontal style bits.
 */
public class NegativeMarginVerticalFieldManager extends Manager
{
    private static final int MAX_EXTENT = Integer.MAX_VALUE >> 1;

    public NegativeMarginVerticalFieldManager( long style ) 
    {
        super( style );
    }
    
    
    protected void sublayout( int maxWidth, int maxHeight )
    {
        Field   field;
        int     width = 0;
        int     height = 0;

        // how much height do we have?
        int heightAvail = maxHeight;
        int widthAvail = maxWidth;

        if( isStyle( Manager.VERTICAL_SCROLL ) && !isStyle( Manager.NO_VERTICAL_SCROLL ) ) {
            heightAvail = MAX_EXTENT;
        }

        int prevMarginBottom = 0;
        int marginTop = 0;
        int marginBottom = 0;
        int marginHorizontal = 0;
        int numFields = this.getFieldCount();

        for( int i = 0; i < numFields; ++i ) {
            field = getField( i );
            
            marginHorizontal =  field.getMarginLeft() + field.getMarginRight();
            marginTop = calculateVerticalMargin( prevMarginBottom, field.getMarginTop() );
            marginBottom = field.getMarginBottom();
            
            layoutChild( field, widthAvail - marginHorizontal, heightAvail - marginTop - marginBottom );
            
            heightAvail -= field.getHeight() + marginTop;
            height += field.getHeight() + marginTop;
            
            prevMarginBottom = marginBottom;
            
            // remember the largest width
            int marginAndWidth = marginHorizontal + field.getWidth() ;
            if( marginAndWidth > width ) {
                width = marginAndWidth;
            }
        }
        height += prevMarginBottom;
        
        if( width < maxWidth && isStyle( Field.USE_ALL_WIDTH ) ) {
            width = maxWidth;
        }
        
        if( height < maxHeight && isStyle( Field.USE_ALL_HEIGHT ) ) {
            height = maxHeight;
        }
        
        setVirtualExtent( width, height );
        
        // Set positions
        int x = 0;
        int y = 0;
        prevMarginBottom = 0;
        for( int i = 0; i < numFields; ++i ) {
            field = getField( i );
            
            marginTop = calculateVerticalMargin( prevMarginBottom, field.getMarginTop() );
            
             if( field.isStyle( Field.FIELD_HCENTER ) ) {
                x = ( width - field.getWidth() ) / 2;
             } else if( field.isStyle( Field.FIELD_RIGHT ) ) {
                x = width - field.getWidth() - field.getMarginRight();
             } else {
                // Field.FIELD_LEFT
                x = field.getMarginLeft();
            }
                
            setPositionChild( field, x, y + marginTop );
            
            y += field.getHeight() + marginTop;
            prevMarginBottom = field.getMarginBottom();
        }
        
        setExtent( Math.min( width, maxWidth ), Math.min( height, maxHeight ) );
    }
    
    /**
     * To account for negative margins
     */
    private int calculateVerticalMargin( int prevMarginBottom, int marginTop ) 
    {
        int max = Math.max( prevMarginBottom, marginTop );
        int sum = prevMarginBottom + marginTop;
        if( sum < max ) {
            max += ( sum - max );
        }
        return max;
    }
    
}    

 

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: java.lang.ArithmeticException

Hi,

 

1. Put breakpoints in the following methods and see if any of them gets non positive value in its parameters: setExtent, setPositionChild & layoutChild.

 

2. Check the syntax on the following line:

private static final int MAX_EXTENT = Integer.MAX_VALUE >> 1;

I don't think you can use bit operation on an Integer object, but I'm not sure...

 

E.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: java.lang.ArithmeticException

I agree with maadani.

 

In addition, I would change the try/catch that is catching this to be a

 

try {

..

} catch (Throwable t) {

t.printStacktrace();

....

}

 

This will give you the stack trace.  You don't get a stacktrace from Exceptions.