What is a null error in code Execution

In my past few years of experience in coding I have faced many times the exception for passing on Null values. Due to which, the compiler or the machine throws this exception out to the screen.


Actually, many times this case has been discussed by other professional developers and part-time developers and they’ve provided the solution to this many times that I think, if you just close your eyes and go for the solution, you’ll find it on the very 3rd web page for the tutorial websites.

What actually this error is?

This error occurs, when you’re passing on a null value. A null value means that there is actuall no value in this variable. Its not even a zero. String is the major case for this exception because other variables sometimes get a zero value, and hide this error. Arrays give out this error, when they’re empty. So in general, anything that has no value, is called to have a null value.

Why does this error come up?

This error generally comes up, when you’re trying to pass a null value as an argument (parameter) to a method (function) that needs an active parameter to work on. When the parameter being passed is empty, null it throws this error. Telling the developer that the variable that he passed is zero.

You can take the example of a shopkeeper. You ask him to give you a packet of biscuits, but you don’t pay him. What would he do? He’d shout at you like, ‘Hey, you didn’t pay for the biscuits!’. Exact is this thing. You don’t pass any value and the compiler complains to you in this manner, telling you that you’ve provided him with nothing.

How to handle this error?

There are many ways of doing this. You can either use a try catch block to see what is the error and then handle it. You can either use a simple condition operator (if else, in this scenario) to detect the value and then do respectively. Or you can use your own idea to check the value is not null. Or you can even prompt the user to enter the value, otherwise don’t trigger the function at all. Or…you can do any thing.

Here are a few samples for that

Using a try catch block

A try catch block is a simple block of code, which executes a block of code in the try part and if there is an error in the code, it passes that error to the catch block for the developer to handle it in a User Friendly manner.

try {
   var something = "";
   // changing the something to an array of characters and 
   // getting the error.
   var characterArray = something.ToCharArray();
   foreach (var item in characterArray) {
     Console.Write("Won't write it, see the null error!");
} catch (System.NullPointerException e) {
   Console.Write("The string was empty!");

In the above example, the catch block would execute because the variable being used was empty. If there would have been some value, the array would have value and thus preventing the error.

if else Block 

This block is basic block of the conditional operation. It checks for a value, and executes either one depending on the true or false result of a condition. It is pretty handy and short for those who don’t have to work with errors but just with values and condition.

if(something != null) { // check for value null
  Console.Write("Not null! String is " + something);
} else {
  Console.Write("Whoops! Null exception.");

See, this was pretty easy to handle.

Now there are some other ideas and logics to work with the code and prevent this error from happening. One of them is to always ask for the value and then continuing, if user doesn’t provide the value, keep him prompted that once he adds value only then he can use that particular feature and all that stuff. Otherwise just to prevent the error. Don’t show stuff to the user.

Where to start from?

Many new developers ask the question to themself and to other fellow developers. The question is simple, for me and for old developers but, it is not easy for the new ones. Question is, ‘Where to start the journey?‘. I had the very same question when I started the journey of development.

I first became a Web Developer, I learnt HTML basics and CSS basics at w3schools.com, but I won’t recommend that website to others. They do have an editor and show some of the code. But they’re not good at explaining the code. They just explain that this code would do this. But not, why this code would do this.

I would recommend using Mozilla Developer Network, if you need an editor (currently, Mozilla doesn’t support this feature. If, when you’re visiting this page, Mozilla adds this, comment please) to test your HTML knowledge, you should go with any online text editor like jsfiddle.net. Otherwise, you can even use your very own NotePad to do this. Here is the process for that

  1. Open the Notepad in the Windows, or any other OS. Just make sure you can write the text in it.
  2. Write the HTML codes, make sure, you’re having the HTML elements properly opened and closed.
  3. Saving the file. Here is the main part, just write the file name and add the format of html or htm, like this

This would create a Browser file for you. Once you click on it, browser would load the file and will show the HTML content to you.

Pretty easy right?

Once you’re good to go with the HTML thing. Try creating something. Like a personal landing page, using all the HTML elements and other stuff like that. After this, you should go for a server-side language and Install the Softwares required and all that kinda thing. I think I had that covered in the other page.

Equivallent to Console.ReadKey() in C#

I remember the code of C++ to pause the Console to show the output. Otherwise the Console exit with either a return type returned or its data type is a void.

system("pause"); // pauses the system, Windows OS only

Well, now that I have to move to C#, I don’t find those System commands. They were a bit handy. But since they’re not built-in, they can be developed in custom. Example code would be

public void pause() {
   Console.WriteLine("Press any key to continue...");

public void main() {

Now it would execute, it would be just the way C++ paused the Console. It would show the dialoge, and then would pause the Console. The return type is void, that’s why it won’t return anything in the Stream as output either.

Pretty simple it was!

Scanning file for particular string or character (Searching)

Last fews days ago, I saw a post on Stack Overflow in which the user wanted to search the file content. Although I didn’t answer that post because it was off-topic for that website. But, now I wanted to share the code for that particular action to the public.

I was working on this project since today morning and I finally came up with the code for that. Actually not only me, some other Helpfull fellows from the Stack Overflow community too. I had a problem in my code but the guys there helped me and made me move on to my right track. A warm thank to them.

Well the code was simple and small in size. Although it was a powerfull code. I used a simple File.txt file in the res folder and then the coding began. Here is the file content.

Ok, here is some text!

Actually this file is created to test the validity of the java application

Java is my favourite programming language. I can say that, because for my every tag

on Stack Overflow, I have no enough score. But for Java I have scored enough!

And I think I can score even more :)

Wish me luck!

Then I wrote the code to search this file for the character or a string the user would input. Definitely I would search for a String, not a character. Because character would always be present in the file somewhere.

The code is

package com.wordpress.thevsorganisation.java.find_words;

import java.io.*;
import java.util.Scanner;

public class Main {
   public static void main (String[] args) throws IOException {
   // Only write the output here!!!
   System.out.print("Write the character to be found in the File: ");
   Scanner sc = new Scanner(System.in);
   String character = sc.next();
   // Find the character
   System.out.println("Searching now...");
   // Close the resource!
 // Get character and all other methods would be here...
 public static void getCharacterLocation (String character) throws IOException {
   System.out.println("File found...");
   File file = new File("res/File.txt");
   Scanner sc = new Scanner(file);
   int lineNumber = 0;
   int totalLines = 0;
   boolean found = false;
   // First get the total number of lines
   while(sc.hasNextLine()) {
   System.out.println("Line looping! For Total Lines variable.");
   int[] lineNumbers = new int[totalLines];
   int lineIndex = 0;
   System.out.println("Searching in each line...");
   sc = new Scanner(file);
   while(sc.hasNextLine()) {
     // Until the end
     /* Get each of the character, I mean string from
     * each of the line... */
     String characterInLine = sc.nextLine().toLowerCase();
     if(characterInLine.indexOf(character.toLowerCase()) != -1) {
       found = true;
     System.out.println("Searching complete, showing results...");
     // All done! Now post that.
     if(found) {
       // Something found! :D
       System.out.print("'" + character + "' was found in the file!");
     } else {
       // Nope didn't found a fuck!
       System.out.println("Sorry, '" + character + "' didn't match any character in file  .");

Share the code, and do remember to ask for more!

Printing the page

I had to go through the printing process as I got a new project from my cousin. On my way, I never ever did such a job which involved printers or hard copy format for the documents. Now that I had the job for that, I started looking up for some resources. I did know that I can use CTRL + P key command to print. But, how to integrate all that inside the HTML document, I was making up some assumptions. And this was what I got.

First thing was to print the page. So for that you can create a button element inside your HTML document and handle it using jQuery for the print event. Here is the code for that:


/* Script part */
$('button').click(function () {

Now the browser would show a simple window, where you would get more options to control the printed version of the page. You can either set them there, or you can use the code to control the page layout even more better. What you would try here is CSS3’s Media Query.

Here is the example, lets assume we’re having a div inside the body element. Which is having the id of printable and all the other body is given the id of non-printable. Now in the CSS you were taught for the usage of Media Query.

You can use this code:

@media print {
  #printable {
    display: block;
  #non-printable {
    display: none;

That was enough here. Now you can use this code to view only the printable part of the page. Which would be helpfull to save the page and to get the desired output.

Some other tips you might need to use:

  1. Always use CSS3 media query to stylize the printable version.
  2. Always keep the width larger so that height of the page is less. Saves the page!
  3. Always keep the note on the page by clicking the button or by Print command. To keep a note on the printed version too.
  4. Keep the font-size short and easy to read.
  5. Use short sentences and good punctuations. It makes the document better to be read. Less words save the space
  6. Always remember to remove the margins and paddings from the body element. Printer would automatically add the margin and paddings to the page which would be enough.

Minimizing the Error (Exception) in Web and Software development

Sometimes we create a code to execute for us. Let’s say, we create a calculator app. In which we have many classes; which include data and methods. Each class has its own specified set of data and methods on which it works.

So, now that you’re having a class of Calculator, and it has some data as value1 and numerous methods. For example you’re having something like a Mathematics class and all of its methods. I won’t use any of the methods that is defined inside a language, because this tutorial is not only for ASP.NET, but is also applicable, to all other programming languages too. Such as Java, C++ and some other Object-oriented languages. I am not aware of PHP, if it applies, do let me know.

  var calc = new Calculator(); // create an calculator class object
  /* variable declaration */
  var val1 = 30;
  var val2 = 15;
  /* expressions and all other activity here... */
  var result = 0;
  result = (val2 * 2) - val1; // we know its gonna be 0

Now, lets continue. We have a simpe program, to calculate the result from these 2 provided variable. Please note that this code was for those, who are using ASP.NET, for those you might be using Java the above code would be changed to the following:

public class fileName {
  public void main () {
    Calculator calc = new Calculator(); // create an calculator class object
    /* variable declaration */
    int val1 = 30;
    int val2 = 15;
    /* expressions and all other activity here... */
    int result = 0;
    result = (val2 * 2) - val1; // we know its gonna be 0

Ummm, why hate C++? Here is the code for that too:

int main () {
  Calculator calc = new Calculator(); // create an calculator class object
  /* variable declaration */
  int val1 = 30;
  int val2 = 15;
  /* expressions and all other activity here... */
  int result = ;
  result = (val2 * 2) - val1; // we know its gonna be 0
  return 0;
  /* can use void to prevent the return line */

Now, you might have come to know about what was done here. There was a simple subtraction, now the user might want to get another result using a division method. Such as:

var some = 2;
var res = some/result;

Boing! Here you get an error, which is known as Exception in the language of Programming. These are the errors that the are generated on the go. In this case it is:


There are many other Exceptions like this, which are annoying. In Web development, they are shown to users which are annoying and are not that much user-friendly. Secondly, in the Software development, these exceptions can break the operation and close the entire software. Too much annoying for the user, that he might uninstall your app.

To control these, you use a block that is known as try catch block. In the try section you write the code to be tried by the Compiler or the machine and in the Catch block you tell compiler what to do if an exception occurs. For example

try {
  /* all your code here to be tried... */
} catch (Exception e) { /* catch all exceptions.. */
  Response.Write("Following error generated: " + e.Message);

This way, you can check if any exception occurs and you can do anything related to it. You can show him an error popup, that might be less annoying or even sometimes helpfull too. You can just say: ‘Sorry, you cannot divide an integer by a zero’, instead of ‘Exception detailed CSxxxx: System.DivideByZeroException, blah and blah and blah’.

Try some other codes and tell me where I might be missing some information.

Windows app development

Well, since the upgrade of Windows 8, developers can now develop apps in HTML and CSS. Windows 8.1 has some other built-in bug fixes too. But this feature came up by the upgrade of OS from Win 7 to Win 8.

Now, we can create a simple and easy HTML software for Windows API. We can stylize it using CSS, and we can work on the events using JavaScript, man please no more JavaScript talk, I am sick of it. jQuery would be used here more oftenly, but still we would need to stick to pure (or native) JavaScript in some places where we would be handling the OS based messages.

So, let us get our selves warmed up for Windows Programming too. It’s just been made easy for us!

For more go to: http://msdn.microsoft.com/en-us/windows/apps/br229512