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: 34
Registered: ‎03-08-2012
My Device: Simulator
My Carrier: Developer

Adding Table view to Application .

Is there anything wrong in this code???

If so kindly correct me where i m wrong.........This code is showing unhandled exception

package mypackage;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.XYPoint;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.Arrays;


public final class MyScreen extends MainScreen 
{
	ButtonField delete;
	Field name;
	LabelField time;
	BitmapField img;
    public MyScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("MyTitle");

        System.out.println("Adding bitmap");
        Bitmap logoBitmap = Bitmap.getBitmapResource("delete.png");       
        img = new BitmapField(logoBitmap, Field.FIELD_HCENTER);

        System.out.println("Adding outer table");
        TableLayoutManager outerTable = new TableLayoutManager(new int[]
           {
        		
            TableLayoutManager.USE_PREFERRED_SIZE,
            TableLayoutManager.SPLIT_REMAINING_WIDTH
           },Manager.VERTICAL_SCROLL);
          
        
        System.out.println("Adding inner table");
        /********************* Adding inner table layout *****************************/
        //now dividing 2nd column into further rows
        TableLayoutManager innerTable = new TableLayoutManager(new int[]
        {
        TableLayoutManager. USE_PREFERRED_SIZE,
        TableLayoutManager.USE_PREFERRED_SIZE }, Manager.USE_ALL_WIDTH);
                
        System.out.println("Adding delete image in inner table");
        innerTable.add(delete);
        
        System.out.println("Adding name field in inner table");
        innerTable.add(name);
        
        System.out.println("Adding time field in inner table");
        innerTable.add(time);
                
        
        ///////////////////////////////////////////////////////////////
        System.out.println("Adding image in outer table");
        outerTable.add(img);
        
        System.out.println("Adding inner table to screen");
        outerTable.add(innerTable);
        
        System.out.println("Adding outer table to screen");
        super.add(outerTable);
                                                      	
    }
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
/////////////////// TableLayoutManger  class /////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

class TableLayoutManager extends Manager
{
    int _columnWidths[];
    int _suggestedColumnWidths[];
    int _rowHeights[];
    int _columnStyles[];

    public static final int USE_PREFERRED_SIZE = 1;
    public static final int USE_PREFERRED_WIDTH_WITH_MAXIMUM = 2;
    public static final int SPLIT_REMAINING_WIDTH = 4;
    public static final int FIXED_WIDTH = 8;

    private static int BITMASK_USE_PREFERRED =
            USE_PREFERRED_WIDTH_WITH_MAXIMUM
            | USE_PREFERRED_SIZE;

    public static int DEFAULT_PADDING = 5;
    int _rows;
    int _columns;
    private int _horizPadding;

    public TableLayoutManager(int columnStyles[], long style)
    {
        this(columnStyles, null, DEFAULT_PADDING, style);
    }

    public TableLayoutManager(int columnStyles[], int columnWidths[],
        int horizontalPadding, long style)
    {
        super(style);

        _horizPadding = horizontalPadding;

        _columnStyles = columnStyles;
        if (_columnStyles == null)
              throw new IllegalArgumentException("No column styles");

        if (columnWidths != null)
        {
            _suggestedColumnWidths = Arrays.copy(columnWidths,
                                     0,columnWidths.length);
            if (_suggestedColumnWidths.length < _columnStyles.length)
            {
                int oldLength = _suggestedColumnWidths.length;
                int increase = columnStyles.length - oldLength;
               _suggestedColumnWidths = Arrays.copy(columnWidths, 0,
                            columnStyles.length);
               Arrays.fill(_suggestedColumnWidths, 0,
                           oldLength, increase);
            }
        }
        else
            _suggestedColumnWidths = new int[_columnStyles.length];
    }

    private Field getField(int x, int y)
    {
        int i = x + (y * _columns);
        if (i >= getFieldCount()) return null;
        return getField(i);
    }

    private boolean isColumnStyle(int value, int flag)
    {
        return ((value) & (flag)) > 0;
    }

    public int getPreferredWidth()
    {
        int numberFields = getFieldCount();
        if (numberFields == 0) return 0;
        int rows = numberFields / _columnStyles.length;
        int prefferedWidth = 0;

        int styles[] = _columnStyles;
        int[] columnWidths = new int[_columns];
        Arrays.fill(columnWidths, -1);

        for (int i = 0; i < _columns; i++)
        {
            // assign the fixed widths
            if (isColumnStyle(styles[i], FIXED_WIDTH))
            {
                columnWidths[i] = _suggestedColumnWidths[i];
            }
            else
            {
                if (isColumnStyle(styles[i], BITMASK_USE_PREFERRED))
                {
                    for (int j = 0; j < rows; j++)
                    {
                        Field field = getField(i, j);
                        if (field != null)
                        {

                            int actualWidth = getPreferredWidthOfChild(field)
                                     + field.getMarginLeft()
                                     + field.getMarginRight();
                            if (isColumnStyle(styles[i],
                                USE_PREFERRED_WIDTH_WITH_MAXIMUM))
                            {
                                actualWidth = Math.min(actualWidth,
                                  _suggestedColumnWidths[i]);
                            }

                            columnWidths[i] =
                               Math.max(actualWidth,columnWidths[i]);
                        }
                    }
                }
            }
        }

        for (int n = 0; n < _columns; n++)
        {
            prefferedWidth += columnWidths[n];
        }
        
        return prefferedWidth;
    }

    public int getPreferredHeight()
    {
        int numberFields = getFieldCount();
        if (numberFields == 0) return 0;

        int rows = numberFields / _columnStyles.length;
        int prefferedHeight = 0;

        int[] rowHeights = new int[rows];
        Arrays.fill(rowHeights, -1);
        
        _columns = _columnStyles.length; 
        for (int i = 0; i < _columns; i++)
        {
            for (int j = 0; j < rows; j++)
            {
                Field field = getField(i, j);
                if (field != null)
                {
                int actualHeight = getPreferredHeightOfChild(field)
                            + field.getMarginBottom()
                            + field.getMarginTop();
                rowHeights[j] = Math.max(actualHeight, rowHeights[j]);
                }
            }
        }

        for (int n = 0; n < rows; n++)
        {
            prefferedHeight += rowHeights[n];
        }
        return prefferedHeight;
    }

    protected void sublayout(int layoutWidth, int layoutHeight)
    {
        int numberFields = getFieldCount();
        if (numberFields == 0) {   
        	if ( this._columnStyles != null ) { //At least one field was added and removed from this manager
        		setExtent( 0 ,  0 );
        	} 
        	return; 
        }
        layoutWidth -= getPaddingLeft() + getPaddingRight();
        layoutHeight -= getPaddingTop() + getPaddingBottom();
        _columns = _columnStyles.length;
        int styles[] = _columnStyles;
        if (isStyle(Field.USE_ALL_WIDTH))
        {
            boolean found = false;
           
for (int n = 0; n < _columns; n++)
           {
               if (styles[n] == SPLIT_REMAINING_WIDTH)
               {
                   found = true;
                   break;
               }
           }
           if (!found)
           {
               styles[_columns - 1] = SPLIT_REMAINING_WIDTH;
           }
        }
        _rows = numberFields / _columns;
        if ((numberFields % _columns) > 0) _rows++;
        _columnWidths = new int[_columns];  // arrays that keep track of
                                            // maximum widths
        _rowHeights = new int[_rows];

        // widths and heights are -1 if unassigned, we use this
        // fact to assign the column widths
        Arrays.fill(_columnWidths, -1);
        Arrays.fill(_rowHeights, -1);

        for (int i = 0; i < _columns; i++)
        {
            // assign the fixed widths
            if (isColumnStyle(styles[i], FIXED_WIDTH))
            {
                _columnWidths[i] = _suggestedColumnWidths[i];
            }
            else
            {
                if (isColumnStyle(styles[i], BITMASK_USE_PREFERRED))
                {
                    for (int j = 0; j < _rows; j++)
                    {
                        Field field = getField(i, j);
                        if (field != null)
                        {
                            layoutChild(field, Math.max(0, layoutWidth
                               - (field.getMarginLeft()
                               + field.getMarginRight())),
                                 Math.max(0,layoutHeight
                               - (field.getMarginBottom()
                               + field.getMarginTop())));

                            int actualWidth = getPreferredWidthOfChild(field)
                                 + field.getMarginLeft()
                                 + field.getMarginRight();
                            int actualHeight = getPreferredHeightOfChild(field)
                                 + field.getMarginBottom()
                                 + field.getMarginTop();

                            if (isColumnStyle(styles[i],
                                  USE_PREFERRED_WIDTH_WITH_MAXIMUM))
                            {
                                actualWidth = Math.min(actualWidth,
                                   _suggestedColumnWidths[i]);
                            }

                            _columnWidths[i] = Math.max(actualWidth,
                                _columnWidths[i]);
                            _rowHeights[j] = Math.max(actualHeight,
                                _rowHeights[j]);
                        }
                    }
                }
            }
        }

        int usedColumnWidth = 0;
        int numUnassignedColumnWidths = 0;

        for (int i = 0; i < _columns; i++)
        {
            if (_columnWidths[i] >= 0)
            {
                usedColumnWidth += _columnWidths[i]
                        + ((i < (_columns - 1)) ? _horizPadding : 0);
            }
            else
            {
                numUnassignedColumnWidths++;
            }
        }

        if (numUnassignedColumnWidths > 0)
        {
            int remainingWidthToAssign = layoutWidth - usedColumnWidth;
            if (remainingWidthToAssign < 0)
            {
                remainingWidthToAssign = 0;
            }

            int splitRemainingWidth = (remainingWidthToAssign -
                                      ((numUnassignedColumnWidths - 1)
                                      * _horizPadding))
                                         / numUnassignedColumnWidths;
            
            int[] evenlySpacedColumnPosition = new int [_columnWidths.length]; 
            Arrays.fill(evenlySpacedColumnPosition, -1);
            
            for (int i = 0; i < _columns; i++)
            {
                int assignedWidth = Math.min(remainingWidthToAssign,
                     splitRemainingWidth);
                if (_columnWidths[i] < 0)
                {
                    _columnWidths[i] = assignedWidth;
                    remainingWidthToAssign -= assignedWidth;
                    evenlySpacedColumnPosition[i] = 1; //mark the current column as assigned
                }
            }
           
            /* 
             * We should assign the rest of the integer division to the evenly spaced columns: 
             * splitRemainingWidth = remainingWidthToAssign  / numUnassignedColumnWidths  
             * */
            if ( this.isStyle(Manager.USE_ALL_WIDTH) && remainingWidthToAssign > 0 ) {
            	for (int i = 0; i < evenlySpacedColumnPosition.length; i++) {
					if ( evenlySpacedColumnPosition[i] == 1 ) {
						_columnWidths[i] += 1;
						remainingWidthToAssign--;
					}
					
					if ( remainingWidthToAssign == 0) break;
				}	 
            }
        }

        int currentRow = 0;
        int currentColumn = 0;
        int y = getPaddingTop();
        for (int n = 0; n < numberFields; n++)
        {            Field field = getField(n);

            if (!isColumnStyle(styles[currentColumn], USE_PREFERRED_SIZE))
            { // do
                // the others we missed from above
                layoutChild(field, Math.max(0, _columnWidths[currentColumn]
                        - (field.getMarginLeft()
                        + field.getMarginRight())),
                        Math.max(0, layoutHeight
                                - y
                                - (field.getMarginBottom()
                                + field.getMarginTop())));
            }

            _rowHeights[currentRow] = Math.max(_rowHeights[currentRow], field
                     .getExtent().height
                     + field.getMarginBottom()
                     + field.getMarginTop());

            currentColumn++;
            if ((n == (numberFields - 1)) || (currentColumn >= _columns))
            {
                // we are at the end of the row or list, so now
                // go and actually do the positioning for each row
                int x = getPaddingLeft();
                for (int i = 0; i < currentColumn; i++)
                {
                    Field field1 = getField(i, currentRow);
                    XYPoint offset = calcAlignmentOffset(field1, Math.max(0,
                             _columnWidths[i]
                                    - (field1.getMarginLeft() + field1
                                            .getMarginRight())), Math.max(0,
                            _rowHeights[currentRow]
                                  - (field1.getMarginBottom()
                                  + field1.getMarginTop())));
                     setPositionChild(field1, x + offset.x
                        + field1.getMarginLeft(), y + offset.y
                        + field1.getMarginTop());
                     x += _columnWidths[i] + _horizPadding;
                }

                y += _rowHeights[currentRow];
                currentColumn = 0;
                currentRow++;
            }
        }

        int totalWidth = 0;
        if (isStyle(Field.USE_ALL_WIDTH))
        {
            totalWidth = layoutWidth;
        }
        else
        {
            for (int i = 0; i < _columns; i++)
            {
                totalWidth += _columnWidths[i]
                      + ((i < (_columns - 1)) ? _horizPadding : 0);
            }
        }

        totalWidth += getPaddingLeft() + getPaddingRight();

        y += getPaddingBottom();
        setExtent(totalWidth, Math.min(y, layoutHeight));
    }

    protected boolean navigationMovement(int dx, int dy, int status, int time)
    {
        int focusIndex = getFieldWithFocusIndex();
        int dirY = (dy > 0) ? 1 : -1;
        int absY = Math.abs(dy);

        for (int y = 0; y < absY; y++)
        {
            focusIndex += _columns * dirY;
            if (focusIndex < 0 || focusIndex >= getFieldCount())
            {
                //#ifdef BlackBerrySDK4.5.0
                this.invalidate(); //ref #217
                //#endif
                return false;
            }
            else
            {
                Field f = getField(focusIndex);
                if (f.isFocusable())
                {
                    f.setFocus();
                }
                else
                    y--; // do it over again
            }
        }

        int dirX = (dx > 0) ? 1 : -1;
        int absX = Math.abs(dx);
        for (int x = 0; x < absX; x++)
        {
            focusIndex += dirX;
            if (focusIndex < 0 || focusIndex >= getFieldCount())
            {
                //#ifdef BlackBerrySDK4.5.0
                this.invalidate(); //Ref #217
                //#endif
                return false;
            }
            else
            {
                Field f = getField(focusIndex);
                if (f.isFocusable())
                {
                    f.setFocus();
                }
                else
                    x--; // do it over again
            }
        }
        
        this.invalidate();
        return true;
    }

    private XYPoint calcAlignmentOffset(Field field, int width, int height)
    {
        XYPoint offset = new XYPoint(0, 0);
        long fieldStyle = field.getStyle();
        long field_x_style = fieldStyle & Field.FIELD_HALIGN_MASK;

        if (field_x_style == Field.FIELD_RIGHT)
        {
            offset.x = width - field.getExtent().width;
        }
        else if (field_x_style == Field.FIELD_HCENTER)
        {
            offset.x = (width - field.getExtent().width) / 2;
        }
        long field_y_style = fieldStyle & Field.FIELD_VALIGN_MASK;
        if (field_y_style == Field.FIELD_BOTTOM)
        {
            offset.y = height - field.getExtent().height;
        }
        else if (field_y_style == Field.FIELD_VCENTER)
        {
            offset.y = (height - field.getExtent().height) / 2;
        }
        return offset;
    }
}

 

Developer
Posts: 16,786
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Adding Table view to Application .

what is the unhandled exception? a nullpointer? illegal state?
any detail message? on what line does it occur?
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 141
Registered: ‎07-13-2011
My Device: BB Bold
My Carrier: Vodafone

Re: Adding Table view to Application .

Hi,

 

I hope you are using eclipse as ur dev environment.

 

If you run the code in debug mode, this should show on which line u r getting this error.

 

Thanks

Rabi

Developer
Posts: 34
Registered: ‎03-08-2012
My Device: Simulator
My Carrier: Developer

Re: Adding Table view to Application .

Yes i m using Eclipse plug in .

and debugger is showing error in adding bitmap to the column of the table.

Is there anything relating to the size of the image used cause i m using a button sized image .

i don't know may be there is any other error in code....

Developer
Posts: 16,786
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Adding Table view to Application .

take a step back and view it from our side: "showing error", would you find this description of the problem helpful?
exceptions have a detail message, and they occur on a certain line of code, both would be more helpful than posting a few hundred lines of unrelated code.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 34
Registered: ‎03-08-2012
My Device: Simulator
My Carrier: Developer

Re: Adding Table view to Application .

sry for trouble.......................Forget the above code....i will just write down the code where error occurs. UNCAUGHT EXCEPTION: Null Pointer exception.

public final class MyScreen extends MainScreen 
{
	ButtonField delete;
	Field name;
	LabelField time;
	BitmapField img;
    public MyScreen()
    {        
        Bitmap logoBitmap =Bitmap.getBitmapResource("delete.png");       
        img = new BitmapField(logoBitmap, Field.FIELD_HCENTER);

//adding outer table i.e. dividing table into two columns
        TableLayoutManager outerTable = new TableLayoutManager(new int[]
           {
        		
            TableLayoutManager.USE_PREFERRED_SIZE,
            TableLayoutManager.SPLIT_REMAINING_WIDTH
           },Manager.VERTICAL_SCROLL);
          
        
        /********************* Adding inner table layout *****************************/
        //now dividing 2nd column into further rows
        TableLayoutManager innerTable = new TableLayoutManager(new int[]
        {
        TableLayoutManager. USE_PREFERRED_SIZE,
        TableLayoutManager.USE_PREFERRED_SIZE }, Manager.USE_ALL_WIDTH);

   /*****************************************/  
  /***************here the error occurs ***********************/
         
        innerTable.add(delete);

/********************************************/
/*****************************************/

        innerTable.add(name);
        innerTable.add(time);
                
        
        ///////////////////////////////////////////////////////////////
        outerTable.add(img);
        outerTable.add(innerTable);
        
        super.add(outerTable);
                                                      	
    }
}

 

Developer
Posts: 16,786
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Adding Table view to Application .

you should be able to find the reason for a nullpointer yourself.
in this case it seems the field "delete" is never constructed, at least not in the code you provide.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 141
Registered: ‎07-13-2011
My Device: BB Bold
My Carrier: Vodafone

Re: Adding Table view to Application .

All the fields are not initialized...... before calling add()

 

ButtonField delete;
	Field name;
	LabelField time;
Highlighted
Developer
Posts: 34
Registered: ‎03-08-2012
My Device: Simulator
My Carrier: Developer

Re: Adding Table view to Application .

thanks simon for pointing out my mistake...........