Hey everyone, long time user of Kirupa and first time poster
Im having a bit of trouble using data from a csv file i import. Im teaching myself as best i can and at first i was importing xml’s to create a dynamic chart. It all worked nicely but the application my company is asking for requires it to read a .txt file which is formatted as a .csv.
Here is my log showing the data is being read and saved in the “csvdata array”:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Graph/testHeights()
at Graph/testFunction()
at Graph()
**********************RESULTS****************************
[0](2)
[0](1) 1
[1](1) 9
[1](2)
[0](1) 2
[1](2) 13
[2](2)
[0](1) 3
[1](2) 17
[3](2)
[0](1) 4
[1](2) 17
[4](2)
[0](1) 5
[1](2) 22
[5](2)
[0](1) 6
[1](2) 22
1,9
2,13
3,17
4,17
5,22
6,22
6
The csv data isnt being traced or noticed in any other functions. Am i calling the array correctly?
Error in debug mode:
Attemping to launch and connect to Player using URL E:\Flash Chart\Src\Graph.swf
[SWF] E:\Flash Chart\Src\Graph.swf - 8235 bytes after decompression
6
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Graph/testHeights()[E:\Flash Chart\Src\Graph.as:151]
at Graph/testFunction()[E:\Flash Chart\Src\Graph.as:143]
at Graph()[E:\Flash Chart\Src\Graph.as:43]
Code:
package {
import fl.transitions.Tween;
import fl.transitions.easing.Strong;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
public class Graph extends Sprite {
var loader:URLLoader = new URLLoader();
var totalBars:int;
var gapWidth:Number=30;
var chartWidth:Number=1020;
var totalBarWidth:Number;
var barWidth:Number;
var heightMultiple:Number;
var highestMultiple:Number;
var csvstr:String;
var csvdata:Array;
var tween:Tween;
var tf:TextFormat = new TextFormat();
var lf:TextFormat = new TextFormat();
//Graph Function
public function Graph():void {
tf.color=0x000000;
tf.size=48;
tf.font="Helvetica";
tf.align="left";
lf.color=0x000000;
lf.size=48;
lf.font="Helvetica";
lf.align="center";
loadCSV();
testFunction();
}
public function loadCSV(url:String = "Chart.txt"):void {
//setup loader to load csv file
try {
loader.load(new URLRequest(url));
} catch (error:Error) {
trace("Unable to load requested document.");
}
loader.addEventListener(Event.COMPLETE, completeHandler);
function completeHandler(event:Event):void {
csvstr=String(event.target.data);
csvdata=CSVtoArray(csvstr);
csvdata.shift();
csvdata.shift();
trace("
**********************RESULTS****************************");
for (var r:int = 0; r < csvdata.length; r++) {
trace("["+r+"]("+csvdata[r].length+") ");
for (var c:int = 0; c < csvdata[r].length; c++) {
trace(" ["+c+"]("+csvdata[r][c].length+") ", csvdata[r][c]);
}
}
trace(csvdata[0]);
trace(csvdata[1]);
trace(csvdata[2]);
trace(csvdata[3]);
trace(csvdata[4]);
trace(csvdata[5]);
var testlength:Number;
testlength=csvdata.length;
trace(testlength);
}
function CSVtoArray(csv:String):Array {
var inQuotes:Boolean=false;
var field:String="";
var finalData:Array = new Array();
finalData.push(new Array());
var line:int=0;
//iterate each character
for (var i:int = 0; i < csv.length; i++) {
var c:String=csv.charAt(i);
var n:String=csv.charAt(i+1);
var ad:Boolean=false;
//if the quote repeats, add the character
if (inQuotes&&c=="\""&&n=="\"") {
field+=c;
}
//if we are inside quotes, add the character
if (inQuotes&&c!="\"") {
field+=c;
}
//if we are not inside quotes...
if (! inQuotes&&c!="\"") {
//if this character is a comma, start a new field
if (c==",") {
finalData[line].push(field);
field="";
//if this character is a newline, start a new line
} else if (c == "
") {
finalData[line].push(field);
finalData.push(new Array());
line++;
field="";
//if this is not leading or trailing white space, add the character
} else if (c != " " && c != " " && c != "\r") {
field+=c;
}
}
//if this is a quote, switch inQuotes
if (c=="\"") {
inQuotes=! inQuotes;
}
}
//add last line
finalData[line].push(field);
//if the last line does not have the same length as the first, remove it
if (finalData[line].length<finalData[0].length) {
finalData.pop();
}//return the resulting array
return finalData;
}
}
public function testFunction():void {
totalBars=6;
trace(totalBars);
totalBarWidth=totalBars+1;
totalBarWidth=gapWidth*totalBarWidth;
totalBarWidth=chartWidth-totalBarWidth;
//totalBarWidth =(chartWidth-(gapWidth*(totalBars+1));
barWidth=totalBarWidth/totalBars;
testHeights();
createBars();
displayNames();
}
public function testHeights():void {
for (var i:int = 0; i < totalBars; i++) {
for (var heightTest:int = 0; heightTest < 100; heightTest++) {
if (heightTest*csvdata*[1]<650) {
heightMultiple=heightTest;
} else if (heightTest * csvdata*[1] >= 650) {
if (highestMultiple<heightMultiple) {
trace(highestMultiple);
break;
} else {
highestMultiple=heightMultiple;
}
}
}
}
}
public function createBars():void {
trace("---Create Bars---");
for (var i:int = 0; i < totalBars; i++) {
var bar:Sprite = new Sprite();
bar.graphics.beginFill(0x25B7E2);
bar.graphics.drawRect(0, 0, barWidth, csvdata*[1] * highestMultiple);
bar.graphics.endFill();
bar.x = gapWidth + (barWidth * i) + (gapWidth*i);
bar.y=694-bar.height;
trace(csvdata);
trace(csvdata);
var val:TextField = new TextField();
val.defaultTextFormat=tf;
val.text=csvdata*[1];
val.x = (barWidth /2) + (barWidth * i) + (gapWidth*i);
val.y=680-48-bar.height;
tween=new Tween(bar,"height",Strong.easeOut,0,bar.height,1,true);
addChild(bar);
addChild(val);
}
trace("------");
}
public function displayNames():void {
trace("---Display Names---");
for (var i:int = 0; i < totalBars; i++) {
var names:TextField = new TextField();
names.defaultTextFormat=lf;
names.text=csvdata*[0];
names.x = (barWidth /5)+(barWidth * i) + (gapWidth*i);
names.y=750-58;
addChild(names);
}
trace("--------------");
}
}
}