1 Replies - 521 Views - Last Post: 10 November 2011 - 03:46 PM Rate Topic: -----

#1 tospace2006  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-November 11

Error with my program

Posted 10 November 2011 - 03:40 PM

My program is supposed to display an array of data

When I compile and run the program I get an error:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at ElevationModel.ReadDem(String inFileName)
at Program.Main()

 


using System;
using System.IO;

class GridPoint
{
    private int row;
    private int column;
    
    public GridPoint( int row, int column)
    {
        this. row = row;
		this. column = column;
    }
    
    public int Row
    {
        get{return row;}
    }
    
	public int Column
	{
	    get{return column;}
	}
	
    public override string ToString()
    {
        return (String.Format("{0,4}{1,4}",row,column));
    }
}

class ElevationModel
{
    private string title;
    private double[ , ] elevations = null;
    private int row;
    private int column;
    
    public ElevationModel(string inFileName)
    {
        ReadDem(inFileName);
    }
    
    public string Title
    {
        get{return title;}
    }
    
    public int Rows
    {
        get{return row;}
    }
    
	  public int Columns
    {
        get{return column;}
    }
    
	public double Elevation(int row, int column)
    {
        return elevations[row,column];
    }
    
    private void ReadDem(string inFileName)
    {
        StreamReader inStream = new StreamReader(inFileName);
        title = inStream.ReadLine();

        string line;
        string [] splittedLine;

		
        row = int.Parse(inStream.ReadLine());
        column = int.Parse(inStream.ReadLine());
        
		for(int i = 0; i< row; i++)
            {
			    line = inStream.ReadLine();
                splittedLine = line.Split(',');

            for (int j = 0; j < column; j++)
               {
                   elevations[i, j] = double.Parse(splittedLine[j]);
               }
            }
        inStream.Close();
            
    }

    public void WriteDem(string outFileName)
    {
        StreamWriter outStream = new StreamWriter(outFileName);
        outStream.WriteLine(title);
        outStream.WriteLine("{0}", row);
        outStream.WriteLine("{0}", column);

        for (int i = 0; i < row; i++)
        {
            for (int h = 0; h < column; h++)
            {
                outStream.WriteLine(elevations [i,h]);
            }
            outStream.WriteLine();
        }
        outStream.Close();
    }

    public GridPoint[] Outliers()
    {
        int rowSize = Rows;
        int columnSize = Columns;
        GridPoint[] temp;
        int countOutliers = 0;

        for (int i = 1; i < rowSize - 1; i++)
        {
            for (int j = 1; j < columnSize - 1; j++)
            {
                double average = (elevations[i - 1, j - 1] + elevations[i, j - 1] +
                   elevations[i + 1, j - 1] + elevations[i - 1, j] + elevations[i + 1, j]
                   + elevations[i - 1, j + 1] + elevations[i, j + 1] + elevations[i + 1, j + 1]) / 8;

                if (Math.Abs(average - Elevation(i, j)) > 100)
                { countOutliers++; }
            }
        }

        temp = new GridPoint[countOutliers];
        int position = 0;

        for (int i = 1; i < rowSize - 1; i++)
        {
            for (int j = 1; j < columnSize - 1; j++)
            {
                double average = (elevations[i - 1, j - 1] + elevations[i, j - 1] +
                    elevations[i + 1, j - 1] + elevations[i - 1, j] + elevations[i + 1, j]
                    + elevations[i - 1, j + 1] + elevations[i, j + 1] + elevations[i + 1, j + 1]) / 8;

                if (Math.Abs(average - elevations[i, j]) > 100)
                {
                    temp[position] = new GridPoint(i, j);
                    position++;
                }
            }
        }
        return temp;
    }
    
    public void SmoothOutliers(int row, int column)
    {
        double average = (elevations[row - 1, column - 1] + elevations[row, column - 1] +
        elevations[row + 1, column - 1] + elevations[row - 1, column] + elevations[row + 1, column]
        + elevations[row - 1, column + 1] + elevations[row, column + 1] + elevations[row + 1, column + 1]) / 8;

        elevations[row, column] = Math.Round(10 * average) / 10;  
    }
	
	public double Area(double elevation)
	{
        int count = 0;
        double area = 0;

        foreach (double a in elevations)
            if (a <= elevation)
                count++;

        area = count * 0.1 * 0.1;
        return area;
	}
	
	public double Volume(double elevation)
	{
        double volume = 0;

        foreach (double s in elevations)
            if (s <= elevation)
                //add up the volumes of each square (0.1 km by 0.1 km)
                //Elevation is in m, convert to km by dividing it by 1000.
                volume += 0.1 * 0.1 * elevation / 1000; //in km cubed.

        return volume;
	}
}

class Program
{
    static void Main()
    {
        string filename = "TahoeDEM.csv";
        ElevationModel model = new ElevationModel(filename);
        GridPoint point = new GridPoint(model.Rows, model.Columns);

        GridPoint[] grid = model.Outliers();

        foreach (GridPoint d in grid)
        {
            Console.WriteLine(d);
        }

    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error with my program

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,659
  • Joined: 02-June 10

Re: Error with my program

Posted 10 November 2011 - 03:46 PM

Hover your mouse over each variable on the line that VIsual Studio stopped on. One of them is null.

See FAQ #5 for debugging tutorials


FAQ (Frequently Asked Questions - Updated Oct 2011

Spoiler

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1