It's all about Java: demonstration
Showing posts with label demonstration. Show all posts
Showing posts with label demonstration. Show all posts

Sunday, 9 May 2021

Demonstration of Java NullPointerException

NullPointerException causes and reasons

Since Java is object oriented programming language, every thing is considered to be an object. and there is always a scope of an object to be null or "No value". 

Since Java supports primitive data types, Java is not considered to be pure Java object oriented programming language. 


Few Notable points about NullPointerException

  1. NullPointerExceptions is unchecked exception
  2. Please read post Exception handling and java keywords before reading this article
  3. Usually if program tries to fetch the value from reference and finds nothing as value, then program throws NullPointerException
  4. 'Zero' is not null
  5. And empty String is not considered to be null
  6. It is not the subclass of RuntimeException class

Demo code that throws NullPointerException 



package com.allabtjava.blog;

public class DemoNullPointerException {

class BeanDemo {
String name;
Integer ID;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getID() {
return ID;
}
public void setID(Integer iD) {
ID = iD;
}
}
public DemoNullPointerException() {
}
public void createNullPointerException() {
BeanDemo bean = new BeanDemo();
bean.setName("All about Java!");
System.out.println(bean.getName());
if(bean.getID() == null) {
throw new NullPointerException();
}else {
System.out.println(bean.getID());
}
}
public static void main(String args[]) {
new DemoNullPointerException().createNullPointerException();
}
}

Output


All about Java!
Exception in thread "main" java.lang.NullPointerException
at com.allabtjava.blog.DemoNullPointerException.createNullPointerException(DemoNullPointerException.java:36)
at com.allabtjava.blog.DemoNullPointerException.main(DemoNullPointerException.java:44)

Friday, 6 October 2017

File Manager/File Explorer with Swing JTree Component

This post is intended to beginners of java swing toolkit programmers.
It is a simple java program which demonstrates the swing based File explorer GUI.





import java.awt.GridLayout;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 * @author NagasharathK
 *
 */
public class FileExplorer extends JFrame {

private JTree fileManagerTree = null;

public FileExplorer() {
initComponents();
}

/**
* Initializes components
*/
private void initComponents() {
this.getContentPane().add(new JScrollPane(createFileManagerTree()));
this.setSize(500, 500);
this.setResizable(true);
this.setTitle("File Manager..");
}

/**
* @return JPanel object which contains other comp...
*/
private JPanel createFileManagerTree() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout());

fileManagerTree = new JTree();
fileManagerTree.setModel(new FilesContentProvider("C:\\"));
panel.add(fileManagerTree);
return panel;
}

class FilesContentProvider implements TreeModel {

private File node;

public FilesContentProvider(String path) {
node = new File(path);

}

@Override
public void addTreeModelListener(TreeModelListener l) {

}

@Override
public Object getChild(Object parent, int index) {
if (parent == null)
return null;
return ((File) parent).listFiles()[index];
}

@Override
public int getChildCount(Object parent) {
if (parent == null)
return 0;
return (((File) parent).listFiles() != null) ? ((File) parent).listFiles().length : 0;
}

@Override
public int getIndexOfChild(Object parent, Object child) {
List<File> list = Arrays.asList(((File) parent).listFiles());
return list.indexOf(child);
}

@Override
public Object getRoot() {
return node;
}

@Override
public boolean isLeaf(Object node) {
return ((File) node).isFile();
}

@Override
public void removeTreeModelListener(TreeModelListener l) {

}

@Override
public void valueForPathChanged(TreePath path, Object newValue) {

}

}

/**
* @param args
* @throws InvocationTargetException
* @throws InterruptedException
* @throws UnsupportedLookAndFeelException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws InvocationTargetException, InterruptedException,
ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
FileExplorer explorerUI = new FileExplorer();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
explorerUI.setVisible(true);
}
});
}
}
 

The above program has one inner class [FilesContentProvider] which serves as a model to the JTree.

The UI has 4 Components: JFrame, JScrollPane, JPanel and JTree.

To Do:

  1. Add Context menu to node based upon the file type
  2. Add Cell renderer to give proper names to the Nodes
  3. Option to Expand whole tree or selected node etc..  


Wednesday, 6 May 2015

Send SMSs with Java

Through this article I want to share some knowledge that I have on sending bulk SMSs through java and .NET applications.
SMSLIB is an open source API which allows the programmers to write java or .NET code to send multiple smss to different phone numbers.
It requires Mobile phone or GSM MODEM along with any SIM.

Please go through this link to get the SMSLIB API.
                                                  http://smslib.org/download/
 

Popular posts

Atomicity with Java Programming Language

 Atomicity with Java What is Atomicity Atomicity, in computer science, is considered to be a property [ALL-OR-NOTHING], that the state of a ...