【问题标题】:AS3 TypeError: Error #1009: Cannot access a property or method of a null object referenceAS3 TypeError:错误 #1009:无法访问空对象引用的属性或方法
【发布时间】:2013-12-03 23:43:20
【问题描述】:

我(以及其他在这里发布问题的人)对AS3 还很陌生。所以我要疯了,试图找出导致这个错误的原因。我有一个“点唱机”播放器,可以在单击按钮时相应地加载歌曲。但这是我的错误:

TypeError:错误 #1009:无法访问 null 的属性或方法 对象引用。在第 09 课_V2_S5_fla::MainTimeline/frame1()

这是我的代码:

import fl.events.SliderEvent;

var snd:Sound;
var channel:SoundChannel;
var trans:SoundTransform;

//create variables to store values for the current song and it's volume and pan        settings.
var currSong:String;
var currVol:Number = .5;
var currPan:Number = 0;

// Array of all the songs in the current playlist.
var songList:Array=new Array("Nothing On You.mp3","Grenade.mp3","Ride.mp3","Pretty       Girl Rock.mp3","Tick Tock.mp3","Dynamite.mp3");

// don't need to see the volume and pan controls until a song is playing
panSlide.visible=false;
volSlide.visible=false;

   //Listeners for the onstage song buttons
song1.addEventListener(MouseEvent.CLICK, chooseSong);
song2.addEventListener(MouseEvent.CLICK, chooseSong);
song3.addEventListener(MouseEvent.CLICK, chooseSong);
song4.addEventListener(MouseEvent.CLICK, chooseSong);
song5.addEventListener(MouseEvent.CLICK, chooseSong);
song6.addEventListener(MouseEvent.CLICK, chooseSong);

//listeners for the volume and pan sliders 
panSlide.addEventListener(SliderEvent.CHANGE, panChange);
volSlide.addEventListener(SliderEvent.CHANGE, volumeChange);


//sets the text field of all of the song buttons to display the names of the songs in      the songList array
for (var i = 0; i < songList.length; i++) {
    var str:String = songList[i] as String;
str = str.replace(".mp3","");
var clip = this["song" + (i + 1)].title;
clip.text = str;
  }

//switch statement to set the current song based on which song button was clicked.

function chooseSong(e:MouseEvent):void {
switch (e.currentTarget.name) {
    case "song1":
        currSong = "../MP3s/"+songList[0] as String;
        break;

    case "song2":
        currSong = "../MP3s/"+songList[1] as String;
        break;

    case "song3":
        currSong = "../MP3s/"+songList[2] as String;
        break;

    case "song4":
        currSong = "../MP3s/"+songList[3] as String;
        break;

    case "song5":
        currSong = "../MP3s/"+songList[4] as String;
        break;

    case "song6":
        currSong = "../MP3s/"+songList[5] as String;
        break;

if (snd != null) {
    channel.stop();
}
snd = new Sound();
snd.load(new URLRequest(currSong));
snd.addEventListener(IOErrorEvent.IO_ERROR, onError);

function onError(e:IOErrorEvent):void {
// Do nothing
}
}
}
channel = new SoundChannel  ;
trans = new SoundTransform(currVol,currPan);
channel = snd.play();
channel.soundTransform = trans;
panSlide.visible = true;
volSlide.visible = true;
//currVolume and pan values are used here for display in the text fields next to    sliders

volLabel.text = "Current Volume " + int(currVol * 100);
panLabel.text = "Current Pan " + int(currPan * 100);

//listens for arrival of ID3 tags
snd.addEventListener(Event.ID3, id3Handler);
 //triggered when id3 tags are available
 //sets info text field to display current song information from id3 tags.
 function id3Handler(event:Event):void {
var id3:ID3Info = snd.id3;
if (id3.songName != null) {
    songTitle.text = id3.songName + "\n";
    info.text="Artist: \n"+id3.artist+"\n \n";
    info.appendText("Album: \n" + id3.album);
    info.appendText("\n\n" + "Available at: \n" + "passionrecords \n.com");
}
}


var format:TextFormat = new TextFormat();
format.font = "Arial Black";
format.color = 0xFFFF00;
format.size = 14;
format.url = "http://www.passionrecords.com/";

info.defaultTextFormat = format;
// uses volume slider value to control volume
function volumeChange(e:SliderEvent):void {
currVol = e.target.value;
volLabel.text = "Current Volume: " + int(currVol*100);
trans.volume = currVol;
channel.soundTransform = trans;
 }


 function panChange(e:SliderEvent):void {
currPan = e.target.value;
panLabel.text = "Current Pan " + int(currPan*100);
trans.pan = e.target.value;
channel.soundTransform = trans;
 }

【问题讨论】:

  • 如果您在调试模式下启动应用程序,那么它应该为您提供错误的行号,这将有助于您跟踪问题。此外,在此处复制代码可能只是一个错误,但您的 chooseSong 函数中出现错误,请将 if 语句移到 switch 之外。
  • 谢谢!我确实修复了该错误,但一旦修复,我得到了一个新错误:错误 #2044:未处理的 IOErrorEvent:。 text=Error #2032: Stream Error.at course09_V2_S5_fla::MainTimeline/chooseSong()[lesson09_V2_S5_fla.MainTimeline::frame1:73] 有什么建议吗?
  • 和第 73 行错误是:snd = new Sound();
  • 你能用你所做的改变更新你的代码吗?请确保它正确缩进以使其更易于阅读。查看此页面以获取有关错误 #2032 curtismorley.com/2008/02/08/actionscript-error-2032 的信息 mp3 的路径可能不正确。

标签: actionscript-3


【解决方案1】:

这是正确的..

错误的第 73 行是:snd = new Sound();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多