Showing posts with label null. Show all posts
Showing posts with label null. Show all posts

06 October 2010

(confusion code) check out what the output..???

public class CDummy
{
       static int toto = 7;

       CDummy getDummy() {
              return null; // pay attention here !
       }

       public static void main(String args[])
       {
              System.out.println("CDummy.");

              CDummy dmy = new CDummy();

              System.out.println(dmy.getDummy().toto);
       }
}

can you guess the output of the above code ? (try not to run the code)

plz replay me....

01 October 2010

Null Layout Manager (swing)

Layout Managers are used in the orginization of Panels and Frames. The proper layout should be chosen to accomodate, frame resizings and use.


Null:
*No Layout, items must be manually positioned and arranged.
This layout should only be used if the window will not and cannot be resized, as the item in the window will stay where they are placed, be that hidden or clumped in one corner of a window.

CODE example:
import java.awt.event.*;
import javax.swing.*;

public class NullLayoutExample extends JFrame {

       public NoLayoutExample(String name) {
              super(name);
              JTextField newItemField;
              JList itemsList;
              JButton addButton;
              JButton removeButton;

              getContentPane().setLayout(null);

              //The text field
              newItemField = new JTextField();
              newItemField.setLocation(12,12);
              newItemField.setSize(150,30);
              getContentPane().add(newItemField);

              //The Add button
              addButton = new JButton(“Add”);
              addButton.setMnemonic(‘A’);
              addButton.setLocation(174, 12);
              addButton.setSize(100,30);
              getContentPane().add(addButton);

              //The List
              itemsList = new JList();
              JScrollPane scrollPane = new JScrollPane(itemsList,
              ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
              ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
              scrollPane.setLocation(12,45);
              scrollPane.setSize(150,150);
              getContentPane().add(scrollPane);

              //The Remove button
              removeButton = new JButton(“Remove”);
              removeButton.setMnemonic(‘R’);
              removeButton.setLocation(174,45);
              removeButton.setSize(100,30);
              getContentPane().add(removeButton);
       }

       public static void main(String[] args) {
              JFrame frame = new NoLayoutExample(“NULL Example”);
              frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
              frame.setSize(286, 230);
              frame.setResizable(false);
              frame.setVisible(true);
       }
}

23 September 2010

A small tip on String to avoid NullPointerException

Problem
======
Sometimes the condition in Java class needs to compare between a constant string and a variable.

For example:
your_string_variable.equals(CONSTANT_STRING);

String str1="ABC";
String str2;
str1.equals("ABC");
str2.equals("ABC"); // this statment thr error


this thing throws NullPointerException if your_variable is not initilized....

Solution
======
To avoid this null pointer exception you can re-write the above statement:

CONSTANT_STRING.equals(your_string_variable);

String str1="ABC";
String str2;
"ABC".equals(str1);
"ABC".equals(str2);